删除一行的C代码
我是一个C noob,我试图制作一个程序来删除一个特定的行。为此,我选择复制源文件的内容,跳过要删除的行。在我的原始代码中,我写道:删除一行的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 } 这给了我很多微笑 最后
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