简体   繁体   English

一个c ++文件的输出作为另一个的输入?

[英]Output of one c++ file as input of the other?

I have a two C++ source code in which one code generates an array for the specified input while other has to use array for execution. 我有两个C ++源代码,其中一个代码为指定的输入生成一个数组,而其他代码则必须使用数组来执行。 I want to know how can I link two C++ file such that output of the first file is the input for second one ? 我想知道如何链接两个C ++文件,以使第一个文件的输出成为第二个文件的输入?

Since they're separate programs, that means they each have a main() function. 由于它们是独立的程序,因此它们各自具有main()函数。 Because of that you can't link them together. 因此,您无法将它们链接在一起。 What you can do, however, is use the shell to redirect the output from one program to the input of another. 但是,您可以使用外壳将输出从一个程序重定向到另一个程序的输入。 For example: 例如:

program1 | program2

The above creates a so-called "pipe". 上面创建了一个所谓的“管道”。 What it does is feed program2 with the output of program1. 它的作用是用program1的输出来输入program2。 Only the standard input and standard output are redirected that way. 这样,只有标准输入和标准输出才被重定向。 In C++ that means std::cin and std::cout. 在C ++中,这意味着std :: cin和std :: cout。 Anything printed on std::cerr or std::clog is not redirected, so make sure to never print errors, warnings or other status/informational messages on std::cout. std :: cerr或std :: clog上打印的任何内容均不会重定向,因此请确保切勿在std :: cout上打印错误,警告或其他状态/信息消息。 Only print the payload data and use std::cerr or std::clog for anything else. 仅打印有效载荷数据,并将std :: cerr或std :: clog用于其他任何内容。

Linux:编译两个文件,并使用终端中的管道将第一个二进制文件的内容推送到第二个二进制文件,否则使用套接字..您可以尝试使用二进制流输出数据,第二个二进制文件可以使用相同的技术推送它成一个数组..我希望可以帮助您..

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

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