You are here:

C++/commad line


Dear Ralph,

Thank you for all the most helpful answers you have given me. I'd like to ask a question that hopefully is not going to take you so much time to answer. It would be great if you could just comment on this topic on save time.

I have a command line program that I run from my C++ script. It accepts the input file name as an argument:

./ent -b -t input.txt

The input file is an array of 1's and 0's, one per line. I need to feed input.txt into ent not all at once but one chunk at a time. Assuming that input.txt is 100 entries long, I need to feed 10 times 10 entries at a time. First 1-10, then 11-20 and so on.

My non-elegant solution is to read input.txt sequentially and when 10 entries are read, write them into a new file and feed the new file into ent. Then delete the file just used. Then when the next 10 entries are read, write them into a new file as well, feed into ent and then delete. This process would repeat 8 more times. While I know this works quite well, it cannot possibly be the best or most elegant solution. Is there something more elegant? Let's assume that I cannot modify ent in any way, and I just have a compiled executable.

Thanks again in advance.


Well the first obvious thing is not to bother deleting the file between each chunk - just delete it at the end if you want to keep things tidy! Opening a file for writing also usually truncates the file if it exists - meaning it erases the previous contents - unless of course you open it for write appending.

The second way would be to pipe your data into ent _if_ the program allows input from stdin - which it seems to, from

   "ent performs a variety of tests on the stream of bytes in infile (or standard input if no infile is specified) ..."

This is the reverse of your previous requirement. You open a pipe to a command for writing (second argument is "w") to ent and write 10 items to the pipe, close it and repeat - so ent is still executed 10 times. Here is a modified version of my earlier MSVC++ popen/pclose example using the MS find command to pipe data into to show the idea:

   #include <iostream> // for std::cout, std::cerr
   #include <stdio.h>  // for FILE, fgets and non-standard _popen and _pclose

   #ifdef _MSC_VER
   # if _MSC_VER >=1300
   inline FILE *popen(const char *command, const char *mode)
       return _popen(command, mode);

   inline int pclose(FILE *stream)
       return _pclose( stream );
   # else
   #  error MSVC++ .NET 2003 or later required.
   # endif

   size_t const NUM_CHUNKS(10);
   size_t const NUM_ITEMS(10);
   char const input_data[NUM_CHUNKS][NUM_ITEMS]
         = { {'1','0','0','1','1','0','1','1','1','0'}
         , {'1','1','0','1','1','1','0','0','1','0'}
         , {'0','1','0','1','1','0','1','0','1','0'}
         , {'1','0','1','1','1','0','1','1','1','0'}
         , {'0','0','0','1','0','0','0','1','1','1'}
         , {'1','0','0','1','0','0','0','1','0','1'}
         , {'0','1','0','1','1','0','0','1','0','0'}
         , {'1','0','1','0','0','0','1','0','1','1'}
         , {'0','1','1','1','0','1','0','0','0','0'}
         , {'1','1','0','1','0','0','0','1','1','1'}
   int main()
       FILE * fpipe;
       for ( size_t chunk=0; chunk<NUM_CHUNKS;++chunk )
         if ( (fpipe=popen("find \"111\"", "wt")) == NULL )
         std::cerr << "Failed to open pipe for command.\n";
         return 1;
         std::cout << "Chunk " << chunk << ":"<< std::endl;
         for ( size_t item=0; item<NUM_ITEMS; ++item )
         fputc(input_data[chunk][item], fpipe);
         fputc('\n', fpipe);
         pclose( fpipe );

In this case one line of '0' and '1' characters are piped into each find invocation, and it only outputs the input if it contains the searched for text - "111" in this case. Like your requirement there are 10 chunks of 10 '0' and '1' characters - in the constant 2D char array input_data. Unlike you requirement each chunk's set of 10 items is placed on the same line - so if you use this as a template remember to modify where you write out your newlines! The output looks like so:

   Chunk 0:
   Chunk 1:
   Chunk 2:
   Chunk 3:
   Chunk 4:
   Chunk 5:
   Chunk 6:
   Chunk 7:
   Chunk 8:
   Chunk 9:

The "Chunk .." lines are output by the program. the 0/1 strings are output by the find program if it contains the search text "111".
Note again this is a quick and dirty example so please remember to tidy up your own implementation taking note of errors and the like.


All Answers

Answers by Expert:

Ask Experts


Ralph McArdell


I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.


My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at


©2016 All rights reserved.