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/

相关文章:

Python 3 详细控制另一个进程的I/O

管道 gsutil 输出到文件

C 列表插入程序 - 运行时出现段错误

c - 我的 execv() 函数在 linux ubuntu 中不起作用

c - 如何在 GCC -> C 中将 flex 和 bison 文件输出链接在一起

c++ - 如何有效地查找大型 vector 中的元素

linux - 是否有随机数的系统调用?

c - 处理像 ls -l/somedir 这样的命令

ruby-on-rails - 我需要一个真正的 UNIX RoR 开发环境

python stdout to file 没有实时响应