Question

I have a program I am writing, I have already completed it, but the requirement/specification for it says that:

In the "Command Prompt", if someone runs your program like this:

Peter David < savednames.txt 

it should print out the names inside savednames.txt that matches each of "Peter" and "David"

I have written the program but my own is interactive, i.e it asks for the file, then the name you want to search for, and then it prints the matches.

It works perfectly fine, but I don't understand what the running of the program in command prompt like this: "Peter David < savednames.txt" means. I am using C++ on Microsoft Windows.

Please I need your help to explain it and how to implement it in my code...is it some kind of operator overloading or ...I don't understand!

Was it helpful?

Solution 2

yourprogramname Peter David < savednames.txt 

means that your program will be called with Peter as first argument, David as second argument, and its standard input will be connected to a stream reading out of savednames.txt.

You just need to read standard input one row at a time, and process it according to the arguments you received.

You just need to modify the code you already have to take the names from the command line (is there a limit? Could there be three names? Or four? You need to consider that) and to read the data from standard input stream, without any need of opening a file.

OTHER TIPS

On the command line, < is used for input redirection. The shell opens the file whose name follows the < and copies its contents into standard input for the program.

So if you called program input.txt, you could open the file and read its contents using std::ifstream or whatever; if you called program < input.txt, you could just read the file's contents from stdin using cin.

You can also do the same for output. Instead of opening a file and writing to it in your code, write to stdout and call your program as program > output.txt.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top