0

I have a text file that has blocks of student data. E.g.:

student number 
name 
number of subjects 
subject code 1
subject code 2
subject....... etc

student number
name
..
.. etc

My problem arises when I want to read in the subject codes, as each block will have a different amount of subjects I need to just read each one in until there are no more subjects. There is where the empty line will be. Therefore I want to create a loop that will read up until the new line.

Btw, each block get saved into an array of structs.

Have tried using strcmp but wasn't sure what I was doing, have no idea otherwise.

for(int j = 0; j < 8; j++) {
  fscanf(fptr,"%s",gRecs->subject[j].subjectcode);
  fscanf(fptr,"%d",&gRecs->subject[j].enrolnmentstat);
  fscanf(fptr,"%d",&gRecs->subject[j].mark);

  printf("%s\n",gRecs->subject[j].subjectcode);
  printf("%d\n",gRecs->subject[j].enrolnmentstat);
  printf("%d\n",gRecs->subject[j].mark);

}

I need to add possibly a while loop within the for loop so that it resets when a empty line is encountered.

The for loop is so that each one is saved into a separate array of structs, 8 is the amount given by the assignment.

Yakov Galka
  • 70,775
  • 16
  • 139
  • 220
kyemoon
  • 3
  • 4
  • 6
    Use [`fgets`](https://en.cppreference.com/w/c/io/fgets), and then parse the string some other way? – Some programmer dude Apr 15 '19 at 07:51
  • 2
    Doesn't `number of subjects` contain the number of following `subject code` lines? – Yakov Galka Apr 15 '19 at 07:54
  • 1
    You seem to have a *number of subjects* line, you could use that as the `for` loop condition. – Simon Doppler Apr 15 '19 at 07:56
  • 1
    Looks like you could loop continually reading with `fgets` and then taking the needed action by simply comparing the first word in the line read -- with each new student block beginning when you encounter the first-word `"student"` (or a blank line if you like). – David C. Rankin Apr 15 '19 at 08:03
  • the posted code does not compile! Please post a [mcve] including a sample of the input for 1 or 2 students – user3629249 Apr 17 '19 at 01:42

1 Answers1

0

As the others suggested you could use fgets for that. And if you don't know the number of subjects that will be listed beforehand, then something like this might work:

#include <stdio.h>
#include <stdlib.h>

#define MAXBUFF 128

int main()
{
    FILE* fp = fopen("stud", "r");

    char line[MAXBUFF];
    if (fp)
    {
        while (1)
        {
            char* c = NULL;
            while ((c = fgets(line, MAXBUFF, fp)) != NULL)
            {
                if (line[0] == '\n')
                {
                    printf("new student info's coming!\n");
                    break;
                }

                /* sscanf(...line...) -> process the line we just read */
                printf("%s", line);
            }

            if (c == NULL)
                break;
        }

        fclose(fp);
    }
    else
    {
        printf("Error opening file...");
    }
}

You'll probably need some kind of iterator in the outer while loop to provide some indexing for your array of structs.

And maybe another one in the inner loop if the subjects stored in the struct are also stored in some kind of an array.

Anyways, this will read the whole file. And if a new line is encountered then it will break the inner loop and you can go on to the next student in the outer loop. Hope this helps in some way!

skyzip
  • 237
  • 2
  • 11
  • Incorrect use of `feof()` in 2 places. Suggest `fgets(line, MAXBUFF, fp); while (!feof(fp))` --> `while(fgets(line, MAXBUFF, fp))` for the first one. See also [Why is “while (!feof(file))” always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feoffile-always-wrong) – chux - Reinstate Monica Apr 15 '19 at 12:47