cin and unknown number of integers
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: cin and unknown number of integers

  1. #1
    Join Date
    Jun 2012
    Posts
    58

    cin and unknown number of integers

    Hi,

    i dont usually write console programs, and i cant seem to find out how one parses an unknown number of arguments with cin.

    the program receives an unknown amount of integers in stdin, and i need to parse them withouth hanging.
    Unfortunately, stdin is a async. stream and blocks, if it doesnt find any integers left.

    I cant use peek() or seek() either, as both are async, too, (which makes me wonder what their exact use is?).

    help?



    note:
    shortened version, this forum decided to swallow my full blown post...

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,294

    Re: cin and unknown number of integers

    Quote Originally Posted by tuli
    the program receives an unknown amount of integers in stdin, and i need to parse them withouth hanging.
    Unfortunately, stdin is a async. stream and blocks, if it doesnt find any integers left.
    Is this supposed to be interactive input? If so, then the user can trigger EOF at the command prompt to get around the wait for more input, or you can just read the input as a string then parse the string. If not, i.e., of the input is being piped from elsewhere, then EOF will be reached anyway, so the wait for more input won't happen.

    Quote Originally Posted by tuli
    I cant use peek() or seek() either, as both are async, too, (which makes me wonder what their exact use is?).
    They can be useful when dealing with files.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Jun 2012
    Posts
    58

    Re: cin and unknown number of integers

    thanks. No, the input is done with a file, but...


    the file looks more like this:

    <header>:x y z,....

    where x,y,z are the integers.
    It may as well be

    <header>:x y
    or
    <header>:x y z a b


    The input file may contain several such lines, so i split them by "\n" first, the process the individual lines, by parsing the header and then reading in the list of integers. So there is not end of file.

    I solved this for now by using the extended string library of a FrameWork.
    I`d still love to know what i can do in such a case, except for parsing the input char by char.

  4. #4
    Join Date
    Oct 2008
    Posts
    1,137

    Re: cin and unknown number of integers

    Quote Originally Posted by tuli View Post
    I`d still love to know what i can do in such a case, except for parsing the input char by char.
    you can use getline and stringstream, or something like this to avoid excessive copying:

    Code:
    std::ifstream     file /* = ... */;
    std::vector<int>  ints;
    
    for( int val; file >> val; )
    {
    	ints.push_back( val );
    
    	if( file.peek() == '\n' )
    	{
    		do_something_on( ints );
    		ints.clear();
    	}
    }
    but there are also boost iostream, boost serialization, regex's, etc ... etc ...
    Last edited by superbonzo; December 2nd, 2012 at 10:39 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center