cod*_*ict 14

没有功能可以为您提供当前行.但是您可以使用ftell函数从文件的开头获取char数量的偏移量.

  • @Donal:他说'char',这就是C拼写'字节'的方式. (10认同)
  • 不是字符数.字节数.当然,C将这些概念结合在一起,但它们*是*不同的. (2认同)
  • 小心。ftell可能不会返回对文本模式流直接有意义的内容。 (2认同)

Tho*_*mas 5

没有获取当前行的功能。您必须自己跟踪它。像这样:

FILE *file;
int c, line;

file = fopen("myfile.txt", "rt");
line = 0; /* 1 if you want to call the first line number 1 */
while ((c = fgetc(file)) != EOF) {
    if (c == '\n')
        ++line;
    /*
        ... do stuff ...
    */
}
Run Code Online (Sandbox Code Playgroud)