1

I'm trying to write a program that :

  • open a txt files
  • read the first word of a line and store it in a variable (or an array) => it'll be a name
  • read the second word of a line and store it in a var/array => it'll be a college department
  • do it for each line so it goes through the whole file

I tried addding printf("%s", variable) to my code to see how it was doing, but the results are very inconsistent : one time nothing happens, one time it got it right, and one time it got it write but with a weird symbol next to it (oh and also, it works only for the Department part of the code, not the Name)

Here's my txt file :

Marie GMP
Rodriguez STID
Latreze GEA
Dos-Santos INFO
Coulon GMP
Fernandez INFO
Munoz GEA
Clerc GEA
Delahaye STID
Fernandes STID
Vallet STID
Martineau STID
Langlois GMP
Zinedan STID
Regnier-Laine INFO
Guillot GMP
Meyer-Alexandre TC
Tanguy GMP
Legros GMP
Herve GMP
Robinski STID
Michaud INFO
Barthelemy GMP
Vincent GEA

and my code :


#include <stdio.h>
#include <stdlib.h>
#define TAILLE_TABLE 100

int main() {
    char charActuel;
    char studentName[TAILLE_TABLE];
    char studentDpt[TAILLE_TABLE];

    printf("Etudiants faisant du sport ET de la musique\n\n");

    FILE* fichierMusique = NULL;
    fichierMusique = fopen("data/MUSIQUE.txt", "r");

    if (fichierMusique != NULL) {
            printf("fichier ouvert\n");
        
        int i = 1;
        do {
            charActuel = fgetc(fichierMusique);
            studentName[i] = charActuel;
            printf("%c", charActuel);
            i = i + 1;
        }
        while (charActuel != ' ');
        printf("%s-\n", studentName);
        i = 1;

        do {
            charActuel = fgetc(fichierMusique);
            studentDpt[i] = charActuel;
            printf("%c", charActuel);
            i = i + 1;
        }
        while (charActuel != '\n');

        
        printf("%s\n", studentDpt);


        fclose(fichierMusique);
        printf("fichier fermé");

    }
    else {
        printf("Impossible d'ouvrir le fichier MUSIQUE.txt");
    }

    return 0;
}

Do you see where the problem comes from ?

  • 1
    One problem is that the name "Meyer-Alexandre" does not fit into an array of size 15. I would change the definition of `TAILLE_TABLE` to at least 100. And you need to put a NUL terminator `'\0'` at the end of the string, or `printf` with `"%s"` won't work. – user3386109 Nov 26 '21 at 08:14
  • i just fixed it thank you, unfortunately i think it's not the only problem –  Nov 26 '21 at 08:17
  • Also, `i` needs to start at 0, since arrays in C are indexed starting at 0. – user3386109 Nov 26 '21 at 08:19
  • i think that was the problem since it works now, thanks –  Nov 26 '21 at 08:21
  • Please create an answer from the working code. Or delete the question. – Yunnosch Nov 26 '21 at 08:30

1 Answers1

1

Since i got a response, here was the problem :

i should have started at 0 and not 1 oh and also i added modifications someone told me to put in so here's the code :

#include <stdio.h>
#include <stdlib.h>
#define TAILLE_TABLE 100

int main() {
    char charActuel;
    char studentName[TAILLE_TABLE];
    char studentDpt[TAILLE_TABLE];

    printf("Etudiants faisant du sport ET de la musique\n\n");

    FILE* fichierMusique = NULL;
    fichierMusique = fopen("data/MUSIQUE.txt", "r");

    if (fichierMusique != NULL) {
        int i = 0;


        printf("fichier ouvert\n\n");
        do {
            charActuel = fgetc(fichierMusique);
            studentName[i] = charActuel;
            i = i + 1;
        }
        while (charActuel != ' ');

        studentName[i-1] = '\0';
        printf("%s\n", studentName);
        i = 0;

        do {
            
            charActuel = fgetc(fichierMusique);
            studentDpt[i] = charActuel;
            i = i + 1;
        }
        while (charActuel != '\n');

        studentDpt[i-1] = '\0';
        printf("%s\n", studentDpt);


        fclose(fichierMusique);
        printf("fichier fermé");

    }
    else {
        printf("Impossible d'ouvrir le fichier MUSIQUE.txt");
    }

    return 0;
}