The store_students() function doesnt work for me.Code:FILE *file_name(char *buf, char *msg, FILE *fp)
{
printf(msg);
if(gets(buf) == NULL) /* check for errors */
error("Greska pri unosu, zavrsavam.\n", 1);
strcat(buf, ".txt");
if((fp = fopen(buf, "r")) == NULL) /* open file with student names */
error("Ne mogu otvoriti navedeni file.\n", 2);
return fp;
}
int number_of_lines(char *buf, FILE *fp)
{
int qsts;
qsts = 0;
while(fgets(buf, MAXBUF, fp) != NULL)
qsts++;
fseek(fp, 0l, 0);
return qsts;
}
void store_students(char *lineptr[], int students, char *buf, FILE *fp)
{
int i;
for(i = 0; i < students; i++) {
fgets(buf, MAXBUF, fp);
lineptr[i] = (char *) malloc(sizeof(strlen(buf) + 1));
strcpy(lineptr[i], buf);
}
fseek(fp, 0l, 0);
}
int main(int argc, char *argv[])
{
char buf[MAXBUF]; /* buffer */
FILE *fp; /* students file */
FILE *predmet; /* questions file*/
int students; /* number of students */
int questions; /* number of questions */
fp = file_name(buf, "Unesite razred: ", fp);
predmet = file_name(buf, "Predmet: ", predmet);
students = number_of_lines(buf, fp);
questions = number_of_lines(buf, predmet);
char **lineptr;
lineptr = (char **) malloc(students);
store_students(lineptr, students, buf, fp);
}
I have no idea why. Am i mallocing wrongly?