需要在C中使用自定义Linux/UNIX命令行utlity"cal"提示

Jon*_*han 0 c linux operating-system system

好的我需要让这个程序cal并排显示" "3个月(一个月前和一个月后),而不是在任何Linux/UNIX中显示一个月.我让它使用" system(customCommand)"三次显示3个日历; 但那时它不是并排的.

我得到了一些使用以下系统调用的提示:

close(..) pipe(..) dup2(..) read(..) and write(..)
Run Code Online (Sandbox Code Playgroud)

我的问题是我应该从什么开始?我是否需要创建子进程而不是将其捕获pipe(..)

如何并排显示三个日历.

恩.

    February 2009          March 2009             April 2009
 S  M Tu  W Th  F  S    S  M Tu  W Th  F  S    S  M Tu  W Th  F  S
 1  2  3  4  5  6  7    1  2  3  4  5  6  7             1  2  3  4
 8  9 10 11 12 13 14    8  9 10 11 12 13 14    5  6  7  8  9 10 11
15 16 17 18 19 20 21   15 16 17 18 19 20 21   12 13 14 15 16 17 18
22 23 24 25 26 27 28   22 23 24 25 26 27 28   19 20 21 22 23 24 25
                       29 30 31               26 27 28 29 30
Run Code Online (Sandbox Code Playgroud)

Pau*_*lin 6

假设您想自己编写而不是使用"cal -3",我会做什么(在伪代码中):

popen three calls to "cal" with the appropriate args

while (at least one of the three pipes hasn't hit EOF yet)
{
  read a line from the first if it isn't at EOF
  pad the results out to a width W, print it
  read a line from the second if it isn't at EOF
  pad the results out to a width W, print it
  read a line from the third if it isn't at EOF
  print it
  print "\n"
}

pclose all three.
Run Code Online (Sandbox Code Playgroud)


小智 6

如果"cal -3"不起作用,只需使用粘贴:)

$ TERM=linux setterm -regtabs 24
$ paste <(cal 2 2009) <(cal 3 2009) <(cal 4 2009)
    febbraio 2009            marzo 2009              aprile 2009
do lu ma me gi ve sa    do lu ma me gi ve sa    do lu ma me gi ve sa
 1  2  3  4  5  6  7     1  2  3  4  5  6  7              1  2  3  4
 8  9 10 11 12 13 14     8  9 10 11 12 13 14     5  6  7  8  9 10 11
15 16 17 18 19 20 21    15 16 17 18 19 20 21    12 13 14 15 16 17 18
22 23 24 25 26 27 28    22 23 24 25 26 27 28    19 20 21 22 23 24 25
                        29 30 31                26 27 28 29 30

$   
Run Code Online (Sandbox Code Playgroud)

(setterm 忽略 -regtabs除非TERM=linuxTERM=con.)