[TriLUG] Scripting & C++ question

S. William Schulz swschulz at astrum.net
Fri Nov 11 19:17:40 EST 2005


On Fri, Nov 11, 2005 at 04:43:02PM -0500, Mark Freeze wrote:
> On 11/11/05, Carolinux.com <bobevans19 at gmail.com> wrote:
> ls /dir/*.txt | yourCplusPlusProgram
> 
> This didn't work for me...
> I thought it might be my program so I wrote the following simple
> program to test it.
> 
> #include <iostream>
> #include <fstream>
> 
> /* arg_test.cpp */
> 
> using namespace std;
> 
> int main(int argc, char *argv[])
> {
>   for(int val=0; val<=argc; val++)
>     cout << argv[val] << endl;
> 
>   return 0;
> }
> 
> This ("./arg_test bob carol ted alice") generates:
> ./arg_test
> bob
> carol
> ted
> alice
> 
> However this (ls /home/mark/programs/cpp | ./arg_test) only generates:
> ./arg_test

I'm not a C++ hack, so excuse me if this is off the mark, but does C++
differentiate between ARGV and STDIN in the same way perl, etc does?

E.g. to handle data piped in a la your 'ls | appname', would be
something like:

	while (<STDIN>) {
		print;
	}

whereas the 'appname arg1 arg2 arg3' would be:

	for ($i=0; $i < @ARGV; $i++) {
		print "$i: $ARGV[$i]\n";
	}


Just a thought...

SW
S






More information about the TriLUG mailing list