I will show only the important part of the problem so it's gonna be easy to understand my doubt. Suppose I am opening a file where each line is a string with a student's name.
Why does it work:
FILE * studentFile;
studentFile = openFile("students.txt", "r");
char ** students;
students = (char**)malloc(100 * sizeof(char*));
if (students == NULL) {
exit(2);
}
int np;
int a = 0;
while (!feof(studentFile)) {
students[a] = (char *)malloc(sizeof(char));
if (students[a] == NULL) {
exit(3);
}
np = fscanf(studentFile, "%s", students[a]);
if (np != EOF) {
++a;
}
}
And it doesn't? :
FILE * studentFile;
studentFile = openFile("students.txt", "r");
char * students;
students = (char*)malloc(100 * sizeof(char));
if (students == NULL) {
exit(2);
}
int np;
int a = 0;
while (!feof(studentFile)) {
np = fscanf(studentFile, "%s", students[a]);
if (np != EOF) {
++a;
}
}