【问题标题】:C Code for Deleting a Line删除行的 C 代码
【发布时间】:2016-08-31 23:16:10
【问题描述】:

我是一个 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);
}

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

【问题讨论】:

    标签: c file io


    【解决方案1】:

    赋值的优先级低于不相等,所以:

    read_char = fgetc(fp) != '\n'
    

    导致read_char 获得01,这是将fgetc() 调用的结果与'\n' 进行比较的结果。

    你需要括号:

     while((read_char = fgetc(fp)) != '\n')
    

    在与'\n'比较之前,会将fgetc()结果分配给read_char

    【讨论】:

      猜你喜欢
      • 2010-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-06
      • 2015-09-10
      • 1970-01-01
      • 2011-08-02
      • 2013-04-02
      相关资源
      最近更新 更多