删除字符c后的文本

ely*_*33t 3 c text input stdio output

我从输入文件中看到一些看起来像这样的文本:

func:
    sll  $t3, $t4, 5       # t1 = (i * 4)
    add  $t3, $a1, $t4     # t2 contains address of array[i]
    sw   $t1, 4($t2)       # array[i] = i
    addi $t2, $t5, 3       # i = i+1
Run Code Online (Sandbox Code Playgroud)

我想"清理"它,并将其输出到另一个文件,如下所示:

func:
    sll  $t3, $t4, 5
    add  $t3, $a1, $t4
    sw   $t1, 4($t2)
    addi $t2, $t5, 3
Run Code Online (Sandbox Code Playgroud)

这是我用来执行此操作的代码块:

    while(fgets(line, 100, input) != NULL)
   {
    int comment = 0;
    for(int x = 0; x < 100; x++)
    {
        if(line[x] == '#')
            comment = 1;

        if(comment == 1)
            line[x] = '\0'; //I know this is incorrect
    }
    fprintf(cleaned, "%s", line);
   }
Run Code Online (Sandbox Code Playgroud)

如何更改该代码块以按我的意愿工作?我搞砸了并尝试了'\n''\ 0'和""的一些东西,但没有一个完全奏效.

提前致谢!

Ola*_*che 5

你可以这样做,但你不需要设置标志.您可以立即截断该行并停止进一步搜索break;

for(int x = 0; x < 100; x++)
{
    if(line[x] == '#') {
        line[x] = '\n';
        line[x + 1] = '\0';
        break;
    }
}
Run Code Online (Sandbox Code Playgroud)