如何在写入stdout时在管道中转换为大写

时间:2016-01-31 14:17:58

标签: c unix pipe fork

我试图将父级传递的字符串作为参数转换为管道大写。我在这种情况下使用它。如何将buf转换为大写? toupper()在这种情况下不起作用。

int fd[2];
char buf[BUF_SIZE];
ssize_t numRead;
//Child
        if(close(fd[1]) == -1){
            fprintf(stderr, "Error closing write end of child\n");
            exit(4);
        }

        for(;;){

            if((numRead = read(fd[0], buf, BUF_SIZE)) == -1){
                fprintf(stderr, "Error reading from pipe (child)\n");
                exit(5);
            }

            if(numRead == 0){
                break;
            }

            if(write(STDOUT_FILENO, buf, numRead) != numRead){
                fprintf(stderr, "Error writing to stdout (child)\n");
                exit(6);
            }
        }

        write(STDOUT_FILENO, "\n", 1);
        if(close(fd[0]) == -1){
            fprintf(stderr, "Error closing read end in child\n");
            exit(7);
        }
        _exit(0);

1 个答案:

答案 0 :(得分:1)

toupper适用于一个字符 - 只需添加一个循环

for (int i = 0; i < BUF_SIZE; ++i) {
   buf[i] = toupper(buff[i]);
}