简体   繁体   English

如何在遇到新行之前读取文件

[英]How do i read a file until a new line is encounterd

I have a text file that has blocks of student data. 我有一个包含学生数据块的文本文件。 Eg: 例如:

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. 尝试过使用strcmp但不确定我在做什么,不知道其他情况。

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. 我需要在for循环中添加一个while循环,以便在遇到空行时重置它。

The for loop is so that each one is saved into a separate array of structs, 8 is the amount given by the assignment. for循环是这样的,每个被保存到一个单独的结构数组中,8是赋值给出的数量。

As the others suggested you could use fgets for that. 正如其他人建议你可以使用fgets 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. 你可能需要在外部while循环中使用某种迭代器来为你的结构数组提供一些索引。

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! 希望这在某种程度上有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM