Paloma Schkrab
Paloma Schkrab

Reputation: 441

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++. 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. 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?

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. Now I know it's a piped cin and cout, but I didn't know how the command line worked and linked to that.

Thank you!

Upvotes: 1

Views: 1009

Answers (2)

Bardi Harborow
Bardi Harborow

Reputation: 1888

argv contains command line arguments. < input.txt is part of the shell syntax and pipes the contents of the file to standard input instead. If you want to read from that, you're looking for cin.

Upvotes: 3

Swordfish
Swordfish

Reputation: 13134

With myApp < input.txt > output.txt you redirect stdin to be read from input.txt and stdout to write to output.txt. So you handle input and output just like you would if a console was attached to your program.

Upvotes: 4

Related Questions