在C中的不同文件中获取标准输出和错误输出
get standard output and error output in different files in C
我正在尝试解决一个问题,因为我正在学习在 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;
}
我的问题是,你知道一些类似问题的例子吗?我找了一些但没有找到,如果您知道其他方法可以帮助我,我也很乐意接受。
我post我现在的代码没有擦除最后一个版本,这样所有人都可以看到进化。
#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
中打印,在 out.txt 中在 command1 和 hello world 上打印成功,在 err.txt 中在 command2 上打印错误,但我不知道不知道为什么打印 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 执行类似的操作。
write(1, ...
写入标准输出。 write(2,...
写入标准错误。所以你所要做的就是重新打开那些文件描述符,以便它们进入指定的文件。例如,
fd1 = open("outputfile", O_WRONLY); dup2(fd1, 1);
fd2 = open("errorfile", O_WRONLY); dup2(fd2, 2);
之后,任何标准输出将进入 outputfile
,任何错误输出将进入 errorfile
。
但首先,您必须解析命令行选项(在 argv
中找到,这是 main
的第一个参数,其长度由 [=16] 的第二个参数给出=]) 找到 --out
和 --err
选项并获取它们的值。
好吧,我终于得到了一个似乎有效的代码,我将命令 cmd2 = write(1, "hello world", 11);
更改为 cmd2 = system("echo $HOME");
因为我很难在文件中写入写入函数,更改我很快就达到了我的 objective:
#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 int cmd1 = system("sleep 5");
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;
}
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"));
const int cmd2 = system("echo $HOME");;
if(( cmd2==(0)))
{
//write(1, "success on command2", strlen("success on command2"));
dup2(out_fd, 1);
write(1, "success on command2 \n", strlen("success on command2 \n"));
}
else 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;
}
只有一个小问题,当我构建一个exe时出现警告说:
warning:implicit 函数声明 'creat'[-Wimplicit-function-declaration]
int out_fd = creat("out.txt",0644);
如果有人知道如何更正它,请告诉我。感谢您的帮助!
我正在尝试解决一个问题,因为我正在学习在 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;
}
我的问题是,你知道一些类似问题的例子吗?我找了一些但没有找到,如果您知道其他方法可以帮助我,我也很乐意接受。
我post我现在的代码没有擦除最后一个版本,这样所有人都可以看到进化。
#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
中打印,在 out.txt 中在 command1 和 hello world 上打印成功,在 err.txt 中在 command2 上打印错误,但我不知道不知道为什么打印 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 执行类似的操作。
write(1, ...
写入标准输出。 write(2,...
写入标准错误。所以你所要做的就是重新打开那些文件描述符,以便它们进入指定的文件。例如,
fd1 = open("outputfile", O_WRONLY); dup2(fd1, 1);
fd2 = open("errorfile", O_WRONLY); dup2(fd2, 2);
之后,任何标准输出将进入 outputfile
,任何错误输出将进入 errorfile
。
但首先,您必须解析命令行选项(在 argv
中找到,这是 main
的第一个参数,其长度由 [=16] 的第二个参数给出=]) 找到 --out
和 --err
选项并获取它们的值。
好吧,我终于得到了一个似乎有效的代码,我将命令 cmd2 = write(1, "hello world", 11);
更改为 cmd2 = system("echo $HOME");
因为我很难在文件中写入写入函数,更改我很快就达到了我的 objective:
#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 int cmd1 = system("sleep 5");
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;
}
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"));
const int cmd2 = system("echo $HOME");;
if(( cmd2==(0)))
{
//write(1, "success on command2", strlen("success on command2"));
dup2(out_fd, 1);
write(1, "success on command2 \n", strlen("success on command2 \n"));
}
else 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;
}
只有一个小问题,当我构建一个exe时出现警告说: warning:implicit 函数声明 'creat'[-Wimplicit-function-declaration]
int out_fd = creat("out.txt",0644);
如果有人知道如何更正它,请告诉我。感谢您的帮助!