[英]C Code for Deleting a Line
我是一个C菜鸟,我试图制作一个删除特定行的程序。 为此,我选择复制源文件的内容,跳过要删除的行。 在我的原始代码中,我写道:
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_char
获得0
或1
,这是将fgetc()
调用的结果与'\\n'
进行比较的结果。
你需要括号:
while((read_char = fgetc(fp)) != '\n')
在与'\\n'
比较之前,会将fgetc()
结果赋值给read_char
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.