-1

Imagine the following command:

./functions < file.txt

In my main() from functions.cpp, how am I able to catch the data given through the '<' ?

I'm pretty sure I have to use std::cinbut besides prompting the user for info I don't know how it can apply here.

std::string file_passed;
std::cin >> file_passed; // prompts user
file_passed << std::cin; // error

It's a noob question, but I can't find the answer anywhere... thanks.

  • `std::cin >> file_passed` does not prompt the user for anything. If a file is piped into STDIN, `std::cin` will read the file data. If you want to know whether STDIN is piped input vs user input, you have to use platform-specific APIs to query the console for that info. See http://stackoverflow.com/questions/1312922/, http://stackoverflow.com/questions/6839508/, etc. – Remy Lebeau Nov 28 '16 at 20:45

1 Answers1

1

This is how you get the input via command line redirection:

std::string temp;
// will read each word in the file in the variable temp on each loop iteration until EOF
while (std::cin >> temp) 
{        
    // you use temp here
    std::cout << temp << std::endl;
}
sharyex
  • 460
  • 1
  • 5
  • 17
  • The issue came from my misconception of piped arguments.. thought doing < file was giving the file and not it's content. As I then tried to do fstream stream -> stream.open(file) and couldn't open it... Thanks for opening my eyes on the problem. – TryingLikeICan Nov 28 '16 at 21:01