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

最初它不需要打印到文件,所以我使用了putchar并且它工作正常
但是现在我使用putc或fputc它将打印所有其他输入。
例:
输入=测试
输出= t s

    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,您使用的是相同的流指针,即inputFile,您想要读取和写入相同的文件?

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

10-11 21:25