Best way to read characters, strings and numbers

This is a discussion on Best way to read characters, strings and numbers within the C Programming forums, part of the General Programming Boards category; I'm being taught to use fflush( stdin ) to clean the buffer, use gets to read strings, etc, etc But ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    2

    Best way to read characters, strings and numbers

    I'm being taught to use fflush( stdin ) to clean the buffer, use gets to read strings, etc, etc

    But it seems these are not the proper methods to read input from keyboard.

    I've made this functions in order to read the input based on this useful recommendations.

    So I would like to know if there is any way to improve them, if you see any kind of problem with them or if something it's already in the c library and therefore it's not necessary a custom function.

    Mostly because I will be using it in all my future c programs.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int getCharacter( char * );
    int getNumber ( int* );
    void getString( char* string );
    
    int main( void ) 
    {
        int number;
        char character;
        char string[ BUFSIZ ];
        
        printf( "Character: " );
        getCharacter( &character );
        
        printf( "Number: " );
        getNumber( &number );
        printf( "String: " );
        getString( string );
        printf( "Character -> %c\n", character );
        printf( "Number -> %d\n", number );
        printf( "String -> %s\n", string );
        
        system( "pause" );    
    }
    
    int getCharacter( char* c )
    {
    	  char buffer[ BUFSIZ ];
    	  fgets(buffer, sizeof( buffer ), stdin);
    	  return sscanf( buffer, "%c", c);
    }
    
    int getNumber ( int* num ) 
    {
    	char buffer[ BUFSIZ ];
    	fgets(buffer, sizeof( buffer ), stdin);
    	return sscanf( buffer, "%d", num);
    }
    
    void getString( char* string )
    {
          char buffer[ BUFSIZ ];
          char *p;
          fgets( buffer, sizeof( buffer ), stdin );
          if ((p = strchr( buffer, '\n')) != NULL)
                *p = '\0';
          strcpy( string, buffer );
    }
    Last edited by VelvetMirror; 05-16-2010 at 02:03 PM.

  2. #2
    Registered User jephthah's Avatar
    Join Date
    May 2010
    Location
    seattle
    Posts
    49
    Quote Originally Posted by VelvetMirror View Post
    I'm being taught to use fflush( stdin ) to clean the buffer, use gets to read strings, etc, etc
    then you need to fire your teacher. the person is clearly incompetent and I'll bet has never written any professional C code.

    here's why you don't flush stdin and why there is absolutely no excuse for anyone teaching you to use gets().



    as to your code, i think you're on the right track. the real questions are what happens when someone:

    (a) inputs too long of a string
    (b) inputs a string instead of a number
    (c) inputs a float instead of an int

    how do your functions handle this? do they refuse to accept the input? and give a warning? or quietly pass back some incomplete or incorrect data?


    .
    Last edited by jephthah; 05-16-2010 at 04:15 PM.

  3. #3
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    520
    I would recommend using a static buffer if you're going to be using these for the rest of your semester.

    @jepththah he is returning the call of sscanf so he could easily check this to determine if it was successful or not.

  4. #4
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    I would use getchar() for getString() and add a size parameter.
    Code:
    void getString (char* string, int size)
    {
      n = 0;  
      while(n < size) 
         if ( (string[n++] = getchar() ) == '\n') 
           break;
      string[n] = '\0';
    }
    The advantage is that it is faster. You don't read and then check with strchr() to find the newline character.
    Also why allocate a buffer with BUFSIZ? Just use string right away. First you don't need to call strcpy() anymore and second it is more correct, in the sense that you make sure you don't overflow string, since string can be less than BUFSIZ and the use of strcpy() will still overflow it. Which makes it kind of pointless since you avoid gets() for that reason. You should use strncpy() to avoid overflows in that sense, though you don't need it here.
    As a general advise, don't underestimate getc() or getchar() when making your own function for input.

    As for getCharacter I really don't think there is anything better than getchar(). If you want to read a character AND clear the input your approach is not "perfect". What if somebody puts more than BUFSIZ characters? The best method is calling something like
    Code:
    while((c=getchar()) != '\n') ;
    or multiple times fgets().

    For getnumber your approach is fine, though you still can avoid the internal buffer by using getchar().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read max value from strings of numbers in text file
    By james890 in forum C++ Programming
    Replies: 14
    Last Post: 04-15-2010, 04:26 PM
  2. Replies: 5
    Last Post: 12-21-2007, 01:38 PM
  3. strings and numbers
    By Granger9 in forum C Programming
    Replies: 3
    Last Post: 09-10-2002, 05:11 PM
  4. convert numbers to strings
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 07-08-2002, 04:43 PM
  5. File Access with Strings and Numbers
    By Mace in forum C Programming
    Replies: 3
    Last Post: 12-01-2001, 04:07 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21