简体   繁体   English

如何使用<和>命令在c ++中获取输入和输出?

[英]How to get the input and output in c++ using the < and > commands?

I'm having a problem to find information about what these command does, what do they return and how to get them on my application in c++. 我在查找有关这些命令的功能,它们返回的内容以及如何在c ++中的应用程序上获取它们的信息时遇到了问题。 That's an exercise and this is how I need to get the input file I'll be reading from and the output file I'll writing to: 这是一个练习,这就是我需要获取要读取的输入文件和要写入的输出文件的方式:

myApp < input.txt > output.txt

I tried to access it using: 我尝试使用以下方式访问它:

int main(int argc, char** argv){
   cout << argv[1] << endl;
}

But I didn't get anything back. 但是我什么也没回来。 I suspect that the argv[1] is an ifstream or something like that. 我怀疑argv [1]是ifstream或类似的东西。 What is it? 它是什么? Or it comes to my main function as a simple string? 还是它以简单的字符串形式出现在我的主要功能中? Should I receive more parameters on int main to get the "<" and ">" as files? 我应该在int main上接收更多参数以获取“ <”和“>”作为文件吗?

The answer suggested doesn't solve my problem. 建议的答案不能解决我的问题。 I wanted to know how the < and > commands work and the link suggested shows how to use piped cin and cout. 我想知道<和>命令如何工作,建议的链接显示了如何使用管道式cin和cout。 Now I know it's a piped cin and cout, but I didn't know how the command line worked and linked to that. 现在,我知道它是通过管道传送的cin和cout,但是我不知道命令行如何工作并与之链接。

Thank you! 谢谢!

argv contains command line arguments . argv包含命令行参数 < input.txt is part of the shell syntax and pipes the contents of the file to standard input instead. < input.txt是shell语法的一部分,并且将文件的内容通过管道传输到标准输入。 If you want to read from that, you're looking for cin . 如果您想阅读一下, 您正在寻找cin

With myApp < input.txt > output.txt you redirect stdin to be read from input.txt and stdout to write to output.txt . 使用myApp < input.txt > output.txt您可以将stdin重定向到从input.txt读取,并将stdout重定向到output.txt So you handle input and output just like you would if a console was attached to your program. 因此,您可以像将控制台连接到程序一样处理输入和输出。

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

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