HOW does WC work ?

when I type in wc file.txt I get something like:

1 1 6 file.txt
BUT when i write wc *.txt I get


 1 1 6 file1.txt
 1 1 1 file2.txt        


Notice the space in front? From my understanding wc parses line by line so if I type

wc *.txt | ./prog -param how does wc output to prog? Does it output line by line info or all together and how can I get rid of that space in front when it reads more than 1 file?

Thanks
You are continuing on topic that you have started in
http://www.cplusplus.com/forum/unices/159802/
and
http://www.cplusplus.com/forum/general/161313/

You should not ask how "wc" woks. You should ask how every program writes to standard output.

You should ask what it means to "pipe", aka to redirect the standard output into the standard input of an another program.

When you use that command line
wc *.txt | ./prog -param

from the point of view of the prog it is exactly same as you would use
./prog -param

and personally type the following as input:
 1 1 6 file1.txt
 1 1 1 file2.txt
 2 2 7 total

You are writing the prog, so your program must read std::cin appropriately.

Every line (of wc output) contains three integers and a filename (or word "total"). Filenames unfortunately can contain whitespace. The integers can be read as formatted input, and formatted input does by default skip the leading whitespace. A getline can read the filename/word.

Forget the wc and concetrate on reading input.
how you skip the leading whitespace?
I need explanations, but thanks for the effort...I still did not get how prog gets output from pipe a) when it is multiple files for wc...all at once? or one line at a time?
The link I did provide does write:
After constructing and checking the sentry object, which may skip leading whitespace, extracts an integer value

Okay, that says just "may".
Another link: http://www.cplusplus.com/reference/istream/istream/operator%3E%3E/
Now we see (int is arithmetic type):
by first constructing a sentry object (with noskipws set to false).

Hmm, what is "noskipws"?
http://www.cplusplus.com/reference/ios/noskipws/

noskipws==false means skipws==true

The code
1
2
int foo;
std::cin >> foo;

thus, by default, skips the leading whitespace and extracts the integer. It does not matter whether the input has
42

or


                   42

In both cases the value of foo becomes 42.

How does program read input from pipe?
How does program read input from a file?
How does program read input when the user types to terminal?

Exactly the same way. The program reads input from an istream object. The stream has characters. Some of them may be newline characters. Sooner of later there might be an end of file, even for what the user types interactively.
thanks...what is a sentry object btw? These terms r too technical as I am still learning and not a researcher..
Topic archived. No new replies allowed.