在C中读取文件时跳过一行

时间:2022-12-22 15:41:12

I have a problem and I haven't found a solution that works. It's really easy, but I don't understand what to do.

我有一个问题,我没有找到一个有效的解决方案。这很容易,但我不明白该怎么做。

I have a file with some lines, like:

我有一个包含一些行的文件,例如:

\#comment

\#comment

icecream 5

pizza 10

pie 7

\#comment

tortillas 5
fajitas 5

And I want that my program just read the lines that don't start with #.

我希望我的程序只读取不以#开头的行。

FILE *pf;
char first [20], second [20];
pf = fopen("config.conf", "r");
if (pf)
{
    while (! feof(pf))
    {
        fscanf(pf, "%s \t ", first);
        while(!strcmp(first,"#")){ `HERE I NEED JUMP TO NEXT LINE`
            fscanf(pf, "%s \t ", first);
        }
        fscanf (pf, "%s \t ", second);
        printf("Food: %s \t Cost: %s \n", first, second);
    }
    fclose(pf);
}
else
    printf( "Errore nell'aprire config.conf\n");

4 个解决方案

#1


4  

There's no real way to get to the next line without reading the line that starts with the #. About all you can do is read that data, but ignore it.

如果没有读取以#开头的行,就没有真正的方法可以到达下一行。您可以做的就是读取该数据,但忽略它。

char ignore[1024];

fgets(ignore, sizeof(ignore), pf);

#2


4  

If you need to read a configuration file, then use the right tool instead of reinventing the wheel.

如果您需要读取配置文件,请使用正确的工具而不是重新发明*。


while(!strcmp(first,"#")

is wrong. You want to filter out the lines which start with a hash sign, and not the ones which are nothing but a hash sign. Also, while(!feof(f)) is wrong. Furthermore, if you're reading line by line, why bother using fscanf() when you can take advantage of fgets() instead?

是错的。您想要过滤掉以哈希符号开头的行,而不是那些只是哈希符号的行。而且,虽然(!feof(f))是错误的。此外,如果你逐行阅读,当你可以利用fgets()时,为什么还要使用fscanf()呢?

All in all, that whole huge while loop can be simplified into something like this:

总而言之,整个巨大的while循环可以简化为这样的东西:

char buf[0x1000];
while (fgets(buf, sizeof(buf), pf) != NULL) {
    if (buf[0] == '#') continue;

    // do stuff
}

#3


2  

You can skip to end of line without using a buffer by applying %*[^\n] format specifier:

您可以通过应用%* [^ \ n]格式说明符而不使用缓冲区跳到行尾:

fscanf(pf, "%*[^\n]");

#4


1  

You might want to use strstr to look for the "#".

您可能希望使用strstr来查找“#”。

See description: http://en.cppreference.com/w/c/string/byte/strstr

请参阅说明:http://en.cppreference.com/w/c/string/byte/strstr

#1


4  

There's no real way to get to the next line without reading the line that starts with the #. About all you can do is read that data, but ignore it.

如果没有读取以#开头的行,就没有真正的方法可以到达下一行。您可以做的就是读取该数据,但忽略它。

char ignore[1024];

fgets(ignore, sizeof(ignore), pf);

#2


4  

If you need to read a configuration file, then use the right tool instead of reinventing the wheel.

如果您需要读取配置文件,请使用正确的工具而不是重新发明*。


while(!strcmp(first,"#")

is wrong. You want to filter out the lines which start with a hash sign, and not the ones which are nothing but a hash sign. Also, while(!feof(f)) is wrong. Furthermore, if you're reading line by line, why bother using fscanf() when you can take advantage of fgets() instead?

是错的。您想要过滤掉以哈希符号开头的行,而不是那些只是哈希符号的行。而且,虽然(!feof(f))是错误的。此外,如果你逐行阅读,当你可以利用fgets()时,为什么还要使用fscanf()呢?

All in all, that whole huge while loop can be simplified into something like this:

总而言之,整个巨大的while循环可以简化为这样的东西:

char buf[0x1000];
while (fgets(buf, sizeof(buf), pf) != NULL) {
    if (buf[0] == '#') continue;

    // do stuff
}

#3


2  

You can skip to end of line without using a buffer by applying %*[^\n] format specifier:

您可以通过应用%* [^ \ n]格式说明符而不使用缓冲区跳到行尾:

fscanf(pf, "%*[^\n]");

#4


1  

You might want to use strstr to look for the "#".

您可能希望使用strstr来查找“#”。

See description: http://en.cppreference.com/w/c/string/byte/strstr

请参阅说明:http://en.cppreference.com/w/c/string/byte/strstr