简体   繁体   English

逐行读取文本文件并检索C中每一行的内容

[英]Read a text file line by line and retrieve the content of each line in C

Say a text file would be like: 说一个文本文件将像:

1, 2345, 7788, 463, ABC
2, 387, 1100, 321, CCC
2, 2222, 22222, 22, DSA

So there are 3 lines in this text file, and my project needs us to realize a function that read this text file line by line, and retrieve each line's content when a specific line is read, and then examine the content of this line. 因此,此文本文件中有3行,我的项目需要我们实现一个函数,该函数逐行读取此文本文件,并在读取特定行时检索每行的内容,然后检查该行的内容。

For example, I would begin to read this file from the first line. 例如,我将从第一行开始读取此文件。 So when this first line is read(1, 2345, 7788, 463, ABC), I will first need to store this line into a string (say it's char[] str), and then I need to break this str into 5 pieces, and each piece contain those five different filed-content separated by the comma, say p1, p2, p3, p4 and p5. 因此,当读取第一行(1、2345、7788、463,ABC)时,我将首先需要将该行存储到字符串中(例如,它是char [] str),然后需要将该str分成5个部分,每个部分都包含用逗号分隔的五个不同的归档内容,例如p1,p2,p3,p4和p5。 Then I need to examine if the p3 is "1100". 然后,我需要检查p3是否为“ 1100”。 If it is, then close this file and continue the program, and if it's not, then I would need to continue to read the second line and do the same thing, and apparently 1100 is the third filed of the second line, so after reading this line the function would terminate. 如果是,则关闭此文件并继续执行程序,如果不是,那么我将需要继续读取第二行并执行相同的操作,显然第二行的第三行是1100,因此在读取后这行函数将终止。

Now could anybody tell me how could I implement it? 现在有人可以告诉我如何实施吗? I'm very new to C and I've searched the internet and got something about fgets(), like: 我是C语言的新手,我已经在网上搜索了有关fgets()的信息,例如:

if (fgets(str, 60, "text.txt")!=NULL){
    puts(str);
}

but here I can't see any hint that this frets() reads the text file line by line. 但是在这里我看不到任何提示,表明此frets()逐行读取文本文件。

Thanks in advance! 提前致谢! :D :d

You want this: 你要这个:

char str[60];
FILE* f = fopen("text.txt", "r");
if (f == NULL) exit(1);
while(fgets(str, 60, f) != NULL){
    fputs(str, stdout);
}
fclose(f);

How to use your code to examine each line? 如何使用您的代码检查每一行?

using code from @Mike, add the following: 使用@Mike中的代码,添加以下内容:

char str[60];
FILE* f = fopen("text.txt", "r");
if (f == NULL) exit(1);
while(fgets(str, 60, f) != NULL){
    if(strstr(str, "1100")  //Add these lines to test for "1100"
    {
          puts("Found 1100!", stdout);//optional
          puts(str, stdout);//optional
          fclose(f);
          return;
    } 
    fputs(str, stdout);
}
fclose(f);
#include <stdio.h>

int main(){
    char str[64];
    int p1, p2, p3, p4;
    char p5[16];
    FILE *fp;
    int i;
    fp=fopen("text.txt", "r");
    while(fgets(str, sizeof(str), fp)!=NULL){
        if(sscanf(str, "%d, %d, %d, %d, %15s", &p1, &p2, &p3, &p4, p5)==5){
            if(p3 == 1100)
                break;
            //do something
            puts(str);
        }
    }
    fclose(fp);

    return 0;
}

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

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