[英]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.