Thread: Getting a variable

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    10

    Getting a variable

    Is there a way to pull any variable type from the input stream, and if the stream is empty then just skip it? I know about gets() but that does char arrays, I want to be able to do numbers. I'm trying to solve a few problems at a programming competition board, and the judge gives the program a long list of numbers in the stream like:

    45 1003 24 1 19
    19 24 145 111 1
    432 123 51 4 4
    etc....

    An arbitrary amount numbers up to 10 are in each line and an arbitrary number of lines between 1 and 1000 are given and with no number like -1 to signal when the stream is done so I never know if 1 line or 1000 lines are in the stream. I REALLY don't want to use gets() and have to parse, that would really make me want to quit...

  2. #2
    max1234
    Guest
    no that does not exist. If it did, socket programming would be a hell of a paradise

  3. #3
    max1234
    Guest
    actually, for your case and knowing that the input flow is in ASCII you could do some scanf but that C not C++

    how hard is it to use strtok() to parse the string by blanks ' ' and to then do a atoi on each word.

    read all the lines until your read an error or until EOF

  4. #4
    max1234
    Guest
    ok, since I am waiting for .NET to finish to install, I'll give you an example of how to use the library to do the dirty work:

    Code:
    #define LEN = 256
    #define CHARSET = " \r\n\t"
    
    int main(int argc, char *argv[], char *envp[]) 
    {
    	int len;
    	int readInt;
    
    	// read up to 256 char
    	while ( fgets(line, LEN-1, stdin) != NULL ) {
    		// make sure the line ends with 0
    		line[LEN-1] = 0;
    
    		for ( char * p = strtok(NULL, CHARSET);	p != NULL; p = strtok(p,CHARSET) ) {
    			// et hop!! here is an integer read from the flow
    			readInt = atoi(p);
    			// do whatever you want with that
    		}
    	}
    
    	// done
    
    	return 0;
    }
    it's up to you to test it

  5. #5
    max1234
    Guest
    ok ok, I wrote a little too fast, I compiled the following one. I didn't test it though. you can do it.
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    #define LEN 256
    #define CHARSET " \r\n\t"
    
    int main(int argc, char *argv[], char *envp[]) 
    {
    	int readInt;
    	char line[LEN];
    
    	// read up to 256 char
    	while ( fgets(line, LEN-1, stdin) != NULL ) {
    		// make sure the line ends with 0
    		line[LEN-1] = 0;
    
    		for ( char * p = strtok(NULL, CHARSET);	p != NULL; p = strtok(p,CHARSET) ) {
    			// et hop!! here is an integer read from the flow
    			readInt = atoi(p);
    			// do whatever you want with that
    		}
    	}
    
    	// done
    
    	return 0;
    }

  6. #6
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Since you're using C++, use fstreams, not FILE *.

    Code:
    #include <iostream>
    #include <fstream>
    
    int main( void ) {
        
        std::ifstream in( "data" );
        int n;
        
        while( !in.eof( ) ) {
        
            in>>n;
            std::cout<<n<<std::endl;
    
        }
    
    }
    Last edited by XSquared; 06-05-2003 at 05:02 PM.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  7. #7
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    But that post applies to C and FILE *s. Are you sure it applies to C++ fstreams?
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    I'm pretty sure eof() for streams has the same characteristics, good and bad, as feof() for FILE *s. Proof of the pudding would be to run your code and see if you get the extra input tacked to the end of the report. I usually do when I use this syntax.

  9. #9
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    LOL wow! that might be the cause of this weird bug in my highscores function Well, what can I say... thank you elad for mentioning "the extra input tacked to the end of the report" Ok, I'll butt out now.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 12:07 PM
  2. Use of variable
    By alice in forum C Programming
    Replies: 8
    Last Post: 06-05-2004, 07:32 AM
  3. Replies: 2
    Last Post: 04-12-2004, 01:37 AM
  4. write Variable and open Variable and get Information
    By cyberbjorn in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2004, 01:30 AM
  5. Variable question I can't find answer to
    By joelmon in forum C++ Programming
    Replies: 3
    Last Post: 02-12-2002, 04:11 AM