1

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!

3
  • ifstream example Commented Nov 6, 2018 at 6:10
  • On my application I'm already reading and writing to a file like this. But instead of having like std::string filename = "Test.b";, when I start the application I need to pass the files as arguments. Like this: myApp < input.txt > output.txt Commented Nov 6, 2018 at 6:14
  • Possible duplicate of Reading piped input with C++ Commented Nov 6, 2018 at 6:15

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Hmmm! So if I do a cout << "test" << endl; it will go to the output.txt file?
@PalomaSchkrab It should, yes.
3

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.