在C语言中读取文件时跳过一行 [英] Skip a line while reading a file in C
本文介绍了在C语言中读取文件时跳过一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我遇到了问题,但没有找到有效的解决方案.这真的很容易,但是我不知道该怎么办.
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");
推荐答案
如果不阅读以#
开头的行,就没有真正的方法可以到达下一行.您几乎可以做的就是读取该数据,但忽略它.
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);
这篇关于在C语言中读取文件时跳过一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文