Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/61.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_Io - Fatal编程技术网

删除一行的C代码

删除一行的C代码,c,file,io,C,File,Io,我是一个C noob,我试图制作一个程序来删除一个特定的行。为此,我选择复制源文件的内容,跳过要删除的行。在我的原始代码中,我写道: while(read_char = fgetc(fp) != '\n') //code to move the cursor position to end of line { printf("%c",read_char); //temporary code to see the skipped characters } 这给了我很多微笑 最后

我是一个C noob,我试图制作一个程序来删除一个特定的行。为此,我选择复制源文件的内容,跳过要删除的行。在我的原始代码中,我写道:

 while(read_char = fgetc(fp) != '\n')   //code to move the cursor position to end of line
{
    printf("%c",read_char);   //temporary code to see the skipped characters
}
这给了我很多微笑

最后,我找到了提供预期输出的代码:

read_char=fgetc(fp);
while(read_char != '\n')   //code to move the cursor position to end of line
{
    printf("%c",read_char);   //temporary code to see the skipped characters
    read_char=fgetc(fp);
}

但这两个代码之间的实际区别是什么?

赋值的优先级低于不相等,因此:

read_char = fgetc(fp) != '\n'
结果是
read\u char
获得一个
0
1
,将
fgetc()
调用的结果与
'\n'
进行比较的结果

你需要括号:

 while((read_char = fgetc(fp)) != '\n')
在与
'\n'
进行比较之前,将
fgetc()
结果分配给
read\u char