Hey bytes community!
this one is a really strange issue I ran into and I'm hoping you all can shine some light on it for me.
This is written in C, not C++
I'm working on a program that reads in information from a txt file and outputs it sorted into another txt file.
the code to read from the file looks like this
first it reads a number on the first line that lets it know how many lines are to follow. then it allocates memory and reads each line and breaks it up.
Here's the problem. When it starts reading the items into the array the first index (0) doesn't get any information and the second index (1) gets the information from the second line in the file (information that should be in the first index(0))
for the life of me I can't figure out why this isn't working, but i have a suspision that the cursor is left on the first line of the file after the fscanf() call and thus the first fgets() is reading the end of that line and not the second... but like i said... i have no idea.
Any help would be appreciated!
this one is a really strange issue I ran into and I'm hoping you all can shine some light on it for me.
This is written in C, not C++
I'm working on a program that reads in information from a txt file and outputs it sorted into another txt file.
the code to read from the file looks like this
Code:
fscanf(fin, "%d", &size); deptList = (struct dept*)(malloc(size*sizeof(struct dept))); for (i=0; i<size; i++) { fgets(line, MAXLEN, fin); strtok(line, delims); strcpy(deptList[i].name, line); p = (char*)strtok(NULL, delims); deptList[i].numFaculty = atoi(p); p = (char*)strtok(NULL, delims); deptList[i].totalSalary = atoi(p); // Not used. p = (char*)strtok(NULL, delims); }
Here's the problem. When it starts reading the items into the array the first index (0) doesn't get any information and the second index (1) gets the information from the second line in the file (information that should be in the first index(0))
for the life of me I can't figure out why this isn't working, but i have a suspision that the cursor is left on the first line of the file after the fscanf() call and thus the first fgets() is reading the end of that line and not the second... but like i said... i have no idea.
Any help would be appreciated!
Comment