[英]C Code for Deleting a Line
I am a C noob, and I was trying to make a program to delete a specific line. 我是一个C菜鸟,我试图制作一个删除特定行的程序。 For this, I chose to copy the contents of the source file, skipping the line intended for deletion. 为此,我选择复制源文件的内容,跳过要删除的行。 In my original code, I wrote: 在我的原始代码中,我写道:
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
}
which gave me lots of smileys. 这给了我很多笑脸。
In the end I found the code which gave the intended output: 最后,我找到了给出预期输出的代码:
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);
}
But what is the actual difference between these two codes? 但这两个代码之间的实际差异是什么?
Assignment has lower priority than not-equal, so: 赋值的优先级低于不等于,因此:
read_char = fgetc(fp) != '\n'
results in read_char
getting a 0
or 1
, the result of comparing the result of the fgetc()
call against '\\n'
. 导致read_char
获得0
或1
,这是将fgetc()
调用的结果与'\\n'
进行比较的结果。
You need parentheses: 你需要括号:
while((read_char = fgetc(fp)) != '\n')
which will assign the fgetc()
result to read_char
before comparing with '\\n'
. 在与'\\n'
比较之前,会将fgetc()
结果赋值给read_char
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.