c - 在C中的不同文件中获取标准输出和错误输出

标签 c pipe output ubuntu-12.04

我正在尝试解决一个问题,因为我正在学习在 C 中使用系统调用。我使用的是 Ubuntu 12.04 64 位。

我有这个声明:

实现一个代码,允许将两个命令中的标准重定向到一个文件(标准输出),并将所有错误输出重定向到另一个文件(错误文件)。文件的名称将使用 out 参数(用于标准输出的名称)和参数 -err(用于错误文件的名称)指定。 语法:after2 [args...] --do [args...] --out --err

声明中没有指定,但我认为我需要使用管道。

我也不能使用 printf、scanf、getc... 系列函数。

我有那个代码:

#include <syscall.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>


int main(void) 
{
    int cmd1 = system("sleep 5");
    int cmd2 = write(1, "hello world", 11);


    if((cmd1 != (0)))
    {
        write(1,"error in command 1",18);
    }
    if((cmd1==(0)))
    {
        write(1, "hello world", 11);
    }
    return EXIT_SUCCESS;
} 

我的问题是,你知道一些类似问题的例子吗?我找了一些但没有找到,如果您知道其他方法可以帮助我,我也很乐意接受。

我发布了我现在的代码,没有删除最后一个版本,这样所有人都可以看到演变。

#include <syscall.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


int main(int argc, char* argv[])
{
    const char* cmd1 = system("sleep 5");
    const char* cmd2 = write(1, "hello world", 11);
    int out_fd = creat("out.txt",0644);
    int err_fd = creat("err.txt",0644);


    if(out_fd < 0){
        write(2, "Can't open file out.txt", strlen("Can't open file out.txt"));
        dup2(err_fd, 2);
        write(2, "Can't open file out.txt \n", strlen("Can't open file out.txt \n"));

        return -1;
        }else if(err_fd < 0){
            write(2, "Can't open file err.txt", strlen("Can't open file err.txt"));
            dup2(err_fd, 2);
            write(2, "Can't open file err.txt \n", strlen("Can't open file err.txt \n"));

            return -1;
        }
    if((cmd1 !=(0)))
    {
        write(2, "There is an error on command1", strlen("There is an error on command1"));
        dup2(err_fd, 2);
        write(2, "Error on command1 \n", strlen("Error on command1 \n"));

        return -1;
        }else if((cmd1==(0)))
        {
            write(1, "success on command1", strlen("success on command1"));
            dup2(out_fd, 1);
            write(1, "success on command1 \n", strlen("success on command1 \n"));
            write(1, "hello world", 11);

            if((cmd2==(0)))
            {
                write(1, "hello world", strlen("hello world"));
                dup2(out_fd, 1);
                write(1, "hello world \n", strlen("hello world \n"));

            }
            if((cmd2 != (0)))
            {
                write(2, "There is an error on command2", strlen("There is an error on command2"));
                dup2(err_fd, 2);
                write(2, "Error on command2 \n", strlen("Error on command2 \n"));

            }
        }
return 0;
}

这次在 shell 中打印 hello worldsuccess on command1There is an error on command2 and success on command1 and hello world in out.txt and There is an error on command2 in err.txt 但我不知道'知道为什么打印 There is an error on command2 同时打印 hello world, any idea

显然我还有一些错误,但我不确定我需要做什么,如果有人能帮助我,我会很高兴!

谢谢。

最佳答案

您可以使用 dup2将 STDIN 和 STDERR 重定向到您打开的任何其他文件。

int out_fd = creat("out.txt",0644);
if(out_fd < 0){
    printf("Could not open file for standard output\n");
}
write(STDOUT_FILENO, "This is printed on the standard output", strlen("This is printed on the standard output"));
dup2(out_fd, STDOUT_FILENO);
write(STDOUT_FILENO, "This goes into out.txt file \n", strlen("This goes into out.txt file \n"));

您可以对 stderr 执行类似的操作。

关于c - 在C中的不同文件中获取标准输出和错误输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30356733/

相关文章:

c - 减慢模拟器速度

c# - 如何减少后台工作程序中使用匿名管道的程序的 CPU 使用率?

c - `extern` 关键字在 c99 标准中是可选的吗?

java - ProcessBuilder 在 do while 循环后打印空输出

Java - 输出不打印?

c - MSP432 中断与编码器计数

C: 无法将文件中的行存储到数组中

c - 不要将 "make test"作为 Travis CI 的一部分运行?

perl - Bash:如何在 "live"管道中进行替换?

c - 如何将stdin通过管道传递给 child 并在C中执行猫