c - putc 会跳过索引 putchar 不会吗?

标签 c

我有一个读取输入并将 ascii 字符打印到文件流的方法。

最初它不需要打印到文件,所以我使用了 putchar 并且它工作得很好 但现在我使用 putc 或 fputc 它会打印所有其他输入。
示例:

输入=测试
输出=ts

    int readFile(char* input,FILE* inputFile)
{
    int anyChanges = 1;
    int iochar = 0;
    int i = 0;

    //credit Foster Chapter 2
    while (( iochar = getc(inputFile) ) != EOF )
    {
        //Returns 1 if no changes made, return 0 if any changes have been made.
        // printf("\t character --> %c",iochar);
        //printf("\nGot to first while loop\n");

        if(iochar != '\n')
        {
            // printf("Got to second loop\n");
            int printedColumns =0;
            input[i] = iochar;
            printf("input array ---> %c\n",input[i]);
            i++;
            printf("i:%d\n",i);
            printf("iochar:%d\n",iochar);
            //This if statement checks for normal ascii characters.
            //If the output is less than 72 it prints it and increments printedColumns.
            if (( ' ' <= iochar ) && ( iochar <= 126 ) )
            {
                fputc(input[i],inputFile);
            }
        }
    }
}

最佳答案

在这里,对于 getc 和 putc,您使用相同的 Stream 指针,即 inputFile ,您想要读取和写入同一文件??

关于c - putc 会跳过索引 putchar 不会吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20140514/

相关文章:

c - 删除逗号之间的白色字符,但不删除逗号内的内容

c - RGBA png alpha 处理

c - 使用 C/C++ 的 LibXml2

c - 使用 select 循环与 fdisset

c - 更新或更改 C 中的按钮标签

有人可以逐行解释这个简短的 C 程序的作用以及如何运行它吗?

c - 我如何找到 igraph 的最短路径的底层 C 代码?

container_of 宏不适用于 char* 或数组成员

编译器过度优化导致数据运行时和调试不一致

c - 在 c 中将用户输入扫描到 .txt 文件中