在C中使用fscanf阅读另一新行

在C中使用fscanf阅读另一新行,c,file-io,scanf,C,File Io,Scanf,假设我要读取一个文件: //it may contain more than 2 lines 12 6 abadafwg 假设我已经读过第一行了,像这样: char input[999]; while(!feof(fpin)) { fscanf(fpin, " %[^\n]", input); //do something here with these numbers //should do something here to read 2nd line } 这是我

假设我要读取一个文件:

//it may contain more than 2 lines
12 6
abadafwg
假设我已经读过第一行了,像这样:

char input[999];
while(!feof(fpin))
{
    fscanf(fpin, " %[^\n]", input);
    //do something here with these numbers
    //should do something here to read 2nd line
}
这是我的问题,我如何读取该文件的第二行?
请帮助QAQ

而不是使用
fscanf(fpin,“%[^\n]”,input)
,建议使用
fgets()
,因为这样可以防止缓冲区溢出。您可以对这两行使用它,然后根据需要进行解析

if (fgets(input, sizeof(input), fpin) == 0) {
  // handle error, EOF
}
int i[2];
int result = sscanf(input,"%d %d", &i[0], &i[1]);
switch (result) {
  case -1: // eof
  case 0: // missing data
  case 1: // missing data
  case 2: // expected
}
if (fgets(input, sizeof(input), fpin) == 0) {
  // handle error, EOF
}
// use the 'abadfwg just read

您提供的代码将读取程序中的所有行(while循环每次迭代一行),而不仅仅是第一行。[我刚刚测试过]

“fscanf(fpin,“%[^\n]”,input)”相同,但带有whitespace@PHIfounder这是一个字符串你试过运行这个吗?好的…但实际上,文件有两行多,我不知道它包含多少行。我只知道第一行是2个数字,第二行是字符串,第三行是2个数字,等等。我需要用这2个数字做一些事情,然后用字符串,所以…顺便说一句,老师建议我们使用fscanf…将2个读取放在一个循环中,以处理N对行,观察结果==EOF(-1)以指示读取文件的完成。如果要使用
fscanf()
,请尝试
result=fscanf(fpin、%d%d'、&i[0]、&i[1])和``result=fscanf(fpin,“%[^\n]”,输入)<代码>。注意:前导空格使用空格,包括任何以前未读取的
\n`。