C语言 fscanning文本文件时如何跳过一行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2799612/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:24:42  来源:igfitidea点击:

How to skip a line when fscanning a text file?

cfilescanf

提问by NLed

I want to scan a file and skip a line of text before reading. I tried:

我想在阅读之前扫描一个文件并跳过一行文本。我试过:

fscanf(pointer,"\n",&(*struct).test[i][j]);

But this syntax simply starts from the first line.

但是这个语法只是从第一行开始。

回答by Zac

I was able to skip lines with scanf with the following instruction:

我可以使用以下指令跳过 scanf 行:

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

The format string matches a line containing any character including spaces. The *in the format string means we are not interested in saving the line, but just in incrementing the file position.

格式字符串匹配包含任何字符(包括空格)的行。该*格式字符串意味着我们不感兴趣,节省了线,但只是在递增文件中的位置。

Format string explanation:
%is the character which each scanf format string starts with;
*indicates to not put the found pattern anywhere (typically you save pattern found into parameters after the format string, in this case the parameter is NULL);
[^\n]means any character except newline;
\nmeans newline;

格式串说明:
%是每个scanf格式串开头的字符;
*表示不要将找到的模式放在任何地方(通常您将找到的模式保存到格式字符串之后的参数中,在这种情况下,参数为 NULL);
[^\n]指除换行符以外的任何字符;
\n表示换行;

so the [^\n]\nmeans a full text line ending with newline.

所以这[^\n]\n意味着以换行符结尾的全文行。

Reference here.

参考这里

回答by WhirlWind

fgets will get one line, and set the file pointer starting at the next line. Then, you can start reading what you wish after that first line.

fgets 将获得一行,并从下一行开始设置文件指针。然后,您可以在第一行之后开始阅读您想要的内容。

char buffer[100];
fgets(buffer, 100, pointer);

It works as long as your first line is less than 100 characters long. Otherwise, you must check and loop.

只要您的第一行少于 100 个字符,它就可以工作。否则,您必须检查并循环。

回答by Hyman

It's not clear what are you trying to store your data into so it's not easy to guess an answer, by the way you could just skip bytes until you go over a \n:

目前尚不清楚您要将数据存储到什么位置,因此很难猜测答案,顺便说一下,您可以跳过字节,直到经过一个\n

FILE *in = fopen("file.txt", "r");

Then you can either skip a whole line with fgetsbut it is unsafe (because you will need to estimate the length of the line a priori), otherwise use fgetc:

然后你可以跳过整行,fgets但它是不安全的(因为你需要先验地估计行的长度),否则使用fgetc

char c;
do {
  c = fgetc(in);
} while (c != '\n');

Finally you should have format specifiers inside your fscanfto actually parse data, like

最后,你应该在你的内部有格式说明符fscanf来实际解析数据,比如

fscanf(in, "%f", floatVariable);

you can refer herefor specifiers.

你可以参考这里的说明符。

回答by Casey

fgetswould work here.

fgets会在这里工作。

#define MAX_LINE_LENGTH 80

char buf[MAX_LINE_LENGTH];

/* skip the first line (pFile is the pointer to your file handle): */
fgets(buf, MAX_LINE_LENGTH, pFile);

/* now you can read the rest of your formatted lines */