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 如何使用fseek将位置指针移动到文件中的下一行_C_File - Fatal编程技术网

C 如何使用fseek将位置指针移动到文件中的下一行

C 如何使用fseek将位置指针移动到文件中的下一行,c,file,C,File,我看到过文件处理程序,其中一个程序使用了fseek,如下所示: /* This example opens a file myfile.dat for reading. After performing input operations (not shown), it moves the file pointer to the beginning of the file. */ #include <stdio.h> int main(void) { FILE *

我看到过文件处理程序,其中一个程序使用了
fseek
,如下所示:

/* This example opens a file myfile.dat for reading.
   After performing input operations (not shown), it moves the file
   pointer to the beginning of the file.
*/

#include <stdio.h>

int main(void)
{
   FILE *stream;
   int result;

   if (stream = fopen("myfile.dat", "r"))
   { /* successful */

   if (fseek(stream, 0L, SEEK_SET));  /* moves pointer to   */
                                  /* the beginning of the file */
   { /* if not equal to 0
             then error ...    */
   }
   else {
       /* fseek() successful  */
   }
}
/*此示例打开一个文件myfile.dat进行读取。
执行输入操作(未显示)后,它会移动文件
指向文件开头的指针。
*/
#包括
内部主(空)
{
文件*流;
int结果;
if(stream=fopen(“myfile.dat”,“r”))
{/*成功*/
如果(fseek(stream,0L,SEEK_SET));/*将指针移动到*/
/*文件的开头*/
{/*如果不等于0
然后错误*/
}
否则{
/*fseek()成功*/
}
}
像这样,可以将文件指针移动到该行之后的下一行

BO_377fc_DM_MISC:8 FC
SG_uu数据3 m11:31|8@0+(1,0)[0|0]“DM


这是两行代码,我想用一种编程方式,当一个人识别数字
377
时,指针现在应该指向下一行,即指向
SG_uuu.DATA3
行,尽管
8fc
后有空格。如果你在C中使用fseek,怎么做呢?

fseek
用于二进制数据,如果你在n文本文件应使用
fgets
getline
(建议使用
getline

有一个关于“fgets()vs getline”的公开讨论,许多人说“fgets不推荐使用”只是gcc宣传他们的特定getline()

fgets()
中可能存在的一个缺陷是,如果读取的是空字节,它不会告诉您任何信息,这是可以通过
getline()
解决的


但是,如果您不喜欢gcc,或者使用了不同的东西,请使用
fgets()
。如果您一直使用gcc,请使用
getline()

试试这段代码。这可能会对您有所帮助。在这里,输入文件的每一行都转换为字符串,因为与复杂的
fseek()相比,字符串操作非常简单
函数。这可能不是完美的答案,但这将是非常简单的解决方案

#include <stdio.h>
#include <string.h>

int main(void)
{
    FILE *stream;
    int result;

    char tmp[100]; // assuming that max length of a line in myfile.dat is 100.

    if (stream = fopen("myfile.dat", "r"))
    { /* successful */
        fscanf(stream, "%100[^\n]", tmp); // assuming that max length of a line in myfile.dat is 100.
        printf("%s", tmp);
        if (strstr(tmp, "377"))
        { // check for 337

            fscanf(stream, "%100[^\n]", tmp); // next line is in the string tmp .

            // continue your program.
            //printf("%s", tmp);
        }
    }
}
#包括
#包括
内部主(空)
{
文件*流;
int结果;
char tmp[100];//假设myfile.dat中一行的最大长度为100。
if(stream=fopen(“myfile.dat”,“r”))
{/*成功*/
fscanf(流,“%100[^\n]”,tmp);//假设myfile.dat中的行的最大长度为100。
printf(“%s”,tmp);
如果(strstr(tmp,“377”))
{//检查337
fscanf(流,“%100[^\n]”,tmp);//下一行在字符串tmp中。
//继续你的计划。
//printf(“%s”,tmp);
}
}
}

fseek
不知道或不关心什么是“行”是。它所能做的就是将读取指针移动到您指定的位置。我需要移动到下一行,从识别数字377开始…我是否应该编写自己的条件以将文件指针移动到下一行?不清楚您为什么要将文件位置移动到下一行。如果您使用
fgets
t读取输入无论如何,文件指针都会移动到下一行。
fseek
的使用更适合于具有固定大小记录的随机访问文件,而不是具有可变长度行的连续文本文件。
fseek
只有在您已经知道目的地的情况下才真正有用。如果您需要扫描下一个换行字符,您将与
fread
fgets
和一些解析代码有关。如果您对文本文件使用
fseek
,可能需要更好的算法,但发布的代码只是一个占位符。特别是,当文件刚打开时,您不需要查找文件的开头。getline是非标准C@Weather Vane编辑了答案。编辑并没有让它变得更好。
getline()
在C标准中不存在。它是GNU C库的一个特定功能,现在是POSIX标准的一部分。任何方面都没有宣传--
getline()
使用起来很方便,但是如果你想要完全的可移植性,就不要使用它。我完全不理解你关于
NULL
的段落。gcc不强制使用
getline
。因此“被卡住了”gcc允许使用标准的
fgets
fgets
返回的
NULL
告诉您没有更多的数据。我对主要问题的评论中显示了惯用用法。如果您使用fgets读取带有NUL字符的文件,则最有可能的结果是每个lne在第一次出现时都被截断NUL。这不是缓冲区溢出,也不是UB。这可能有点奇怪,但posix(至少)不能保证非文本文件可以由测试实用程序处理。此外,C标准也不能保证文本文件中的NUL可以被读取。那么你在为谁辩护呢?