Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C-查找我打开的文本文件的当前行_C_File_Data Manipulation - Fatal编程技术网

C-查找我打开的文本文件的当前行

C-查找我打开的文本文件的当前行,c,file,data-manipulation,C,File,Data Manipulation,我需要一个解决方案,以找到文本文件中当前使用的行。 我打开它时说: FILE *fp; fp=fopen(“data.txt”, “ r+“); 要跟踪包含当前正在处理的文本行的文件中的哪一行,可以使用count变量。这里有一个简单的例子 while (fgets(buffer, BUFLEN, fp) != NULL) { printf("Line %d: %s", line, buffer); if (buffer[strlen(buffer)-1] != '\n')

我需要一个解决方案,以找到文本文件中当前使用的行。 我打开它时说:

FILE *fp;
fp=fopen(“data.txt”, “ r+“);

要跟踪包含当前正在处理的文本行的文件中的哪一行,可以使用count变量。这里有一个简单的例子

while (fgets(buffer, BUFLEN, fp) != NULL)
{
    printf("Line %d: %s", line, buffer);
    if (buffer[strlen(buffer)-1] != '\n') 
    // fgets didnt read an entire line, so increment the file pointer to
    // the start of the next line. also, output the newline since it wasn't
    // part of buffer above
    {
        int ch;
        do
        {
            ch = fgetc(fp);
        }
        while (ch != '\n' && ch != EOF);
        putchar('\n');
    }
    line++;
}

例如,创建一个unsigned int类型的变量,并用零值初始化它

然后每次读入新行时,将该变量增加1:

unsigned int currentLine = 0;
... 
// read line
currentLine++;

“当前使用的线路”是什么意思?如果您需要规格,我会发送完整的代码。是的,您需要添加更多详细信息。请阅读。我的文件光标位于第二行(例如),我想获取该数字供以后使用。@IsaacChabot您必须自己保留该数字。只要用你从文件中读到的每一行增加它。谢谢你。我也需要它。