0

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!

Paloma Schkrab
  • 301
  • 1
  • 4
  • 13
  • [ifstream example](https://en.cppreference.com/w/cpp/io/basic_ifstream#Example) – rsy56640 Nov 06 '18 at 06: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 – Paloma Schkrab Nov 06 '18 at 06:14
  • Possible duplicate of [Reading piped input with C++](https://stackoverflow.com/questions/5446161/reading-piped-input-with-c) – Bardi Harborow Nov 06 '18 at 06:15

2 Answers2

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.

Bardi Harborow
  • 1,803
  • 1
  • 28
  • 41
3

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.

Swordfish
  • 12,971
  • 3
  • 21
  • 43