TryingLikeICan TryingLikeICan - 5 days ago 5
C++ Question

C++ - Retrieving data from angle bracket when running file

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::cin
but 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.

Answer

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;
}
Comments