[TriLUG] Scripting & C++ question

karl thiele kdthiele at nc.rr.com
Sun Nov 13 01:28:05 EST 2005


S. William Schulz wrote:

>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
>
>GOOD CATCH. A pipe used in the example given is "piped" to standard in. You will also not run into the limit placed upon the limited number of arguments on a command line, if you write to standard out piped to standard in.
>  
>






More information about the TriLUG mailing list