c - 写入标准输出时如何在管道中转换为大写

标签 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);

最佳答案

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

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

关于c - 写入标准输出时如何在管道中转换为大写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35114826/

相关文章:

linux - 使用 grep 列出目录中的条目

c - 虚拟 tty 中的管道使用

c - Unix 套接字 : when to use bind() function?

c - 尝试将多维数组分配给结构

c - 如何在C中重新分配char数组元素的值

C 奇怪的统计 st_mode

linux - 使用 awk 对常见元素进行分组

c - 通过管道从多个子进程写入父进程

C Unix - 循环中的 fork()、execl() 和管道

c - Wolfram Alpha 的指数怎么能这么快?