I need to simulate the Linux command "cal -3
", which displays the calendar for 3 months side by side. What I need right now is to get my implementation, using pipes, working. I've been told that I can't use fork()
, but rather I should use dup2()
, write()
, read()
and close()
to call system("myCustomCommand")
three times. Right now my program does not display the calendar side by side.
我需要模拟Linux命令“cal -3”,它会并排显示3个月的日历。我现在需要的是使用管道来实现我的实现。我被告知我不能使用fork(),而是应该使用dup2(),write(),read()和close()来调用系统(“myCustomCommand”)三次。现在我的程序不会并排显示日历。
I am trying to use pipes and ran into a problem. Here is what I am trying:
我正在尝试使用管道并遇到问题。这是我正在尝试的:
int pfd[2];
int p; //for pipe
int d; //for dup2
const int BSIZE = 256;
char buf[BSIZE];
p = pipe(pfd);
if (p == -1) { perror("pipe"); exit(EXIT_FAILURE); }
if (p == 0)
{
d = dup2(pfd[1], 0);
close(pfd[1]);
nbytes = read (pfd[1], buf , BSIZE);
close(pfd[0]);
exit(EXIT_SUCCESS);
}
else
{
close(pfd[0]);
write(pfd[1], "test\n", BSIZE);
close(pfd[1]);
exit(EXIT_SUCCESS);
}
Unfortunately, this code does not display anything. Could you please help me out with this?
不幸的是,此代码不显示任何内容。你能帮我解决这个问题吗?
3 个解决方案
#1
This looks like homework, so I'll give you a way to approach the problem:
这看起来像家庭作业,所以我会给你一个解决问题的方法:
- Get it working with one calendar, reading in one line at a time and writing to stdout.
- Now store each line in an array of strings, and print out each line once you get the whole calendar read in.
- Get it working with three calendars, storing the results of each into three separate arrays of strings, then printing out all three (not next to each other).
- Instead of printing out all of the lines from one calendar, then all of the lines from the next calendar, etc., print out the first line from each calendar, then the second line from each calendar, etc.
- Fiddle around with the formatting until it looks right.
让它使用一个日历,一次读取一行并写入标准输出。
现在将每一行存储在一个字符串数组中,并在读完整个日历后打印出每一行。
让它使用三个日历,将每个日历的结果存储到三个独立的字符串数组中,然后打印出所有三个(不是彼此相邻)。
不是打印出来自一个日历的所有行,而是来自下一个日历的所有行等,打印出每个日历的第一行,然后打印每个日历的第二行等。
摆弄格式,直到它看起来正确。
#2
Displaying three calendars at once has nothing to do with forking processes and really you don't need to get in to pipes and stuff.
一次显示三个日历与分叉过程无关,实际上你不需要进入管道和东西。
What you want to use is the ncurses library to do special control of your output.
您想要使用的是ncurses库来对输出进行特殊控制。
#3
Why not using FILE *fp = popen("my command", "r");
, reading the output into an array of strings, repeating that three times and concatenating arrays properly?
为什么不使用FILE * fp = popen(“我的命令”,“r”); ,将输出读入一个字符串数组,重复三次并正确连接数组?
#1
This looks like homework, so I'll give you a way to approach the problem:
这看起来像家庭作业,所以我会给你一个解决问题的方法:
- Get it working with one calendar, reading in one line at a time and writing to stdout.
- Now store each line in an array of strings, and print out each line once you get the whole calendar read in.
- Get it working with three calendars, storing the results of each into three separate arrays of strings, then printing out all three (not next to each other).
- Instead of printing out all of the lines from one calendar, then all of the lines from the next calendar, etc., print out the first line from each calendar, then the second line from each calendar, etc.
- Fiddle around with the formatting until it looks right.
让它使用一个日历,一次读取一行并写入标准输出。
现在将每一行存储在一个字符串数组中,并在读完整个日历后打印出每一行。
让它使用三个日历,将每个日历的结果存储到三个独立的字符串数组中,然后打印出所有三个(不是彼此相邻)。
不是打印出来自一个日历的所有行,而是来自下一个日历的所有行等,打印出每个日历的第一行,然后打印每个日历的第二行等。
摆弄格式,直到它看起来正确。
#2
Displaying three calendars at once has nothing to do with forking processes and really you don't need to get in to pipes and stuff.
一次显示三个日历与分叉过程无关,实际上你不需要进入管道和东西。
What you want to use is the ncurses library to do special control of your output.
您想要使用的是ncurses库来对输出进行特殊控制。
#3
Why not using FILE *fp = popen("my command", "r");
, reading the output into an array of strings, repeating that three times and concatenating arrays properly?
为什么不使用FILE * fp = popen(“我的命令”,“r”); ,将输出读入一个字符串数组,重复三次并正确连接数组?