-1

假设我有一个文件要阅读:

//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

4

2 回答 2

0

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

于 2013-06-25T07:33:21.320 回答
0

建议不要使用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
于 2013-06-25T02:26:13.577 回答