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 ?