简体   繁体   English

从C UNIX中的外部文件(或控制文件)中插入两个文件

[英]Piping two files from an outside file (or a control file) in C unix

Hi i am having trouble wrapping my head around this concept. 嗨,我很难绕过这个概念。

I need to execute two files lets say ac & bc from a control file called control.c 我需要执行两个文件,从一个名为control.c的控制文件中说ac&bc

I want to pipe the output of ac to the input of bc 我想将ac的输出传递到bc的输入

I understand that file descriptors(fd): fd 0 = stdin fd 1 = stdout 我知道文件描述符(fd):fd 0 = stdin fd 1 = stdout

I understand that creating a pipe requires an array of integers (two fds) 我知道创建管道需要整数数组(两个fds)

e.g.
zpipe[2]
pipe(zpipe)

i want to execute the files a and b and pipe the output of a to the input of b. 我想执行文件a和b,并将a的输出传递到b的输入。

however i am confused at the concept as opening or reading said files would create a new fd. 但是我对这个概念感到困惑,因为打开或读取所说的文件会创建一个新的fd。 I understand the fd of a (eg fda) can be set to 0 by calling fda = dup2 (fd,0). 我知道可以通过调用fda = dup2(fd,0)将a的fd(例如fda)设置为0。 Would this not break the pipe as 0 is now closed? 现在关闭0不会破坏管道吗?

if i called 如果我打电话

execvp("a.c", args)
execvp("b.c", args)

how would i ensure these files are correctly piped and waited for? 我将如何确保正确传输这些文件并等待它们?

You would probably want to use a pipe and fork to get the desired results. 您可能希望使用管道和叉子来获得所需的结果。 Take a look at the answers to this question . 看看这个问题的答案。

Figured it out. 弄清楚了。 i was attempting to run execvp with user defined programs. 我试图用用户定义的程序运行execvp。 where i called execvp("ac, args) 我在这里叫execvp(“ ac,args)

I should of been calling 我应该打电话

execvp("./a", args) execvp(“ ./ a”,args)

Rookie mistake my apologies 新秀误会我的道歉

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM