Khalid Talakshi
Khalid Talakshi

Reputation: 41

C++ Reading a filename from stdin

So I'm trying to build a version of wc, and one of the key abilities of this program is that you can specify files in two ways:

wc file.txt

and

wc < file.txt

I have figured out how to implement the first way, but I am struggling with the second way. How could I approach this?

Upvotes: 0

Views: 460

Answers (1)

tadman
tadman

Reputation: 211560

The way tools like this work, which includes many others like grep, is if there's no arguments on the command-line that specify file-names, input is read from std::cin.

In a simple sense, if argc is 1 then you have only the executable name as an argument so no files were specified. In a more practical situation you'd use something like an argument parser which may interpret various flags, but which will give a count of non-flag arguments.

Upvotes: 1

Related Questions