![](/img/trans.png)
[英]For a file pointer in C, how to measure the distance between the current position to the end of file?
[英]How to find the current line position of file pointer in C?
如何获取文件指针的当前行位置?
没有功能可以为您提供当前行。 但是您可以使用ftell函数从文件的开头获取char数量的偏移量。
获取当前行没有任何功能; 你必须自己跟踪它。 像这样的东西:
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 ...
*/
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.