Thread: best choice for input???

  1. #1
    Registered User
    Join Date
    Aug 2002

    best choice for input???

    I've been reading a lot of posts concering the use of gets, fgets, scanf, getchar....all discussing the issue of portability. while scanf is the easier choice to use (for those who are lazy), why and when should one be used as opposed to the other??? I realize that certain situations warrant the use of certain functions, but we've all come across situations where we can choose which operation would be the most "portable"...any takers?

    .:just pondering:.
    eat, drink and be merry. for tomorrow: we party.

  2. #2
    Personally, unless the situation calls for a specific function. I will choose between getch(), and scanf(). If I only need to get one character/numeric input, then getch() is the choice. If not then I go for scanf(). I, personally, find them both portable.

    For almost 100% portability, I believe scanf would be your choice.

  3. #3
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    I almost always use fgets. It takes the input as a string and then you can use functions like atoi and atof to convert it to an int or float.

    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    >>>fscanf() / scanf()
    are ANSI. But the are really designed for use with input that is in a guaranteed format/layout. The user can never be trusted to provide such data Both of these functions will cause buffer overflows in char arrays if things don't go according to plan. Also, when reading strings using %s, input is delimited by white space. A common mistake is for the programmer to use scanf("%s", name) to get a users name, and then the user enters "Jon Doe". In this case, only the word Jon is stored in the variable name, and Doe is left in the input stream. In an attempt to ensure these functions have done what you expect, always check their return codes, which, if they worked, will be equal to the number of input arguments for which values were successfully scanned and stored.

    is ANSI. This will get you 1 character (in the form of an int) from an input stream. Because it's only one character, there's no buffer overflow. This is a good function to use if you want to have close control over what you are reading in, because you can check each and every character and take appropriate action. The action itself maybe to append the character to a char array, write it to an output stream, or just ignore the newly input character altogether.

    is ANSI. Again, this gets one character from input, but this time from stdin only.

    is ANSI. This function is bad news, do not use it. All it does is simply reads into a char array, but as with the scanf functions, there's no buffer checking.

    is ANSI. This reads from an input stream, into a char array, but also performs length checking. You tell it how big the buffer is, and it will only put that many characters into it. The input is delimited by (reading stops when we hit a..) newline characters (or EOF). So, it's a good function to use to get data line by line.

    is ANSI. The getc() function is equivalent to fgetc(), except that it may be implemented as a macro.

    What do I use? Well, it depends on the situation Take your pick from the functions available to you, making sure it'll do the job you want it to.

    (If I've missed out any functions here, let me know and I'll add them to the list).
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Aug 2002


    wonderful::: hammer's input is the kind that i find the most...hmm...enjoyable: lots and lots of info.
    eat, drink and be merry. for tomorrow: we party.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. still problems with ceasar shift
    By trevordunstan in forum C Programming
    Replies: 2
    Last Post: 09-14-2008, 01:49 AM
  2. Is this a good choice?
    By EvilDeeds in forum C++ Programming
    Replies: 8
    Last Post: 12-07-2007, 12:21 PM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  4. Menu Help
    By CJ7Mudrover in forum C Programming
    Replies: 7
    Last Post: 03-09-2004, 09:59 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM