C stdout到使用dup的文件



我正在编写一个程序,要求用户输入s、f或0作为用户输入。S将预定义的消息打印到系统,f将该预定义的消息写入用户给定的文件作为参数。0终止程序。

我需要使程序只有一个写入stdout的写入语句。我必须用dup2将stdout中打印的预定义消息复制到一个文件中。

现在这两个进程必须通过输入来分隔(f写入文件,s写入stdout(,所以我不知道如何在switch语句中实现它。

当您输入f时,它不应该将任何内容打印到stdout。

这是我的代码:

#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <sys/wait.h>
#define BUFFER_SIZE 128
#define PERMS 0666
int main(int argc, char *argv[])
{
char outBuffer[BUFFER_SIZE] = "This is a messagen";
int count;
int fd;
char input =0;
int a;
int c;
int k[2];
pipe(k);
if(argc!=2){
printf("Provide an valid file as an argumentn");
exit(1);
}
if((fd = open(argv[1],O_CREAT|O_WRONLY|O_APPEND,PERMS)) == -1)
{
printf("Could not open filen");
exit(1);
}

printf("0 to terminate, s to write to stdout, f to write to filen");
do{
scanf(" %c", &input);
switch(input)
{
case 'f':
if((c = fork()) == 0)
{
close(k[0]);
dup2(fd,1);
close(k[1]);
}
else
{
close(k[0]);
close(k[1]);
wait(0);
wait(0);
}
break;
case 's':
write(1,outBuffer,strlen(outBuffer));
break;
default:
printf("Invalid Choicen");
}
}while(input != '0');
close(fd);
return 0;
}

f现在只在按下s(打印outBufer消息(或触发默认开关后将stdout引导到文件

我想要的输出:

f
f
s
This is a message
s
This is a message
f

文件包含:

This is a message
This is a message
This is a message

下面是我对这个问题的看法;我删除了fork()pipe(),这两个都是为了在Windows上进行本机编译(gcc -g -std=c11 -Wall -c main.c && gcc main.o -o main.exe(,因为我认为它们不是必需的。当我使用MSYS2编译/测试时,我必须显式地刷新stdout,否则直到退出才显示信息性消息。

我重构了switch块,这样我们就可以用breakcontinue控制主循环,我把它们重写为while(1)

#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#define BUFFER_SIZE 128
#define PERMS 0666
int main(int argc, char *argv[]) {
char outBuffer[BUFFER_SIZE] = "This is a messagen";
int fd, fd_f, fd_s;
char input = 0;
if(argc != 2){
printf("Provide an valid file as an argumentn");
exit(1);
}
if((fd_f = open(argv[1], O_CREAT|O_WRONLY|O_APPEND, PERMS)) == -1){
printf("Could not open filen");
exit(1);
}
printf("0 to terminate, s to write to stdout, f to write to filen");
fflush(stdout); // messages are not printed on MSYS2 without explicitly flushing
fd_s = dup(STDOUT_FILENO); // set fd_s to stdout with dup(1)
fd = dup(STDOUT_FILENO);
// if fd is not initialised, calling dup2 will close(0) [i.e. close(stdin)]!
while(1) {
scanf("%c", &input);
fflush(stdin);
if(input == '0') break;
else if(input == 'f') dup2(fd_f, fd); // close(fd) and alias fd_f to fd
else if(input == 's') dup2(fd_s, fd);
else {
printf("Invalid Choicen");
fflush(stdout);
continue;
}
write(fd, outBuffer, strlen(outBuffer));
}
close(fd_f);
close(fd);
return 0;
}

输出:

标准输出

>main.exe file.txt
0 to terminate, s to write to stdout, f to write to file
f
f
s
This is a message
s
This is a message
f
p
Invalid Choice
0
>
# ./main.exe file.txt
0 to terminate, s to write to stdout, f to write to file
f
f
s
This is a message
f
p
Invalid Choice
0

file.txt

This is a message
This is a message
This is a message

最新更新