gets vs fgets

This is a discussion on gets vs fgets within the C Programming forums, part of the General Programming Boards category; Hi. I am implementing a program which has check my input for length and if it is over 80 characters ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    8

    gets vs fgets

    Hi.
    I am implementing a program which has check my input for length and if it is over 80 characters it needs to output an error saying that the input is over 80 characters, and if there is no input (if only enter is pressed at the prompt) it needs to simply end the program with no output.

    this is why I did not limit my array size, but I checked if the input is over 80 characters big and in that case output an error and end the program, and in a case of size 0 I only end the program.

    but I have a problem with array overrun.

    I got a suggestion to use fgets over gets to deal with it.

    But I am not sure how to implement fgets to read input from the keyboard.

    As well as that, from how I understood the function fgets , it returns NULL if there is no input (only 'enter') or on any other error (which includes the case if the input is over 80 characters).

    is there any other way I can implement my error checking?

    THANX!

  2. #2
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    From Dinkum C99 Library Reference
    fgets
    char *fgets(char *restrict s, int n, FILE *restrict stream);
    The function reads characters from the input stream stream and stores them in successive elements of the array beginning at s and continuing until it stores n-1 characters, stores an NL character, or sets the end-of-file or error indicators. If fgets stores any characters, it concludes by storing a null character in the next element of the array. It returns s if it stores any characters and it has not set the error indicator for the stream; otherwise, it returns a null pointer. If it sets the error indicator, the array contents are indeterminate.
    Here's a link:
    http://www.phim.unibe.ch/comp_doc/c_...ONS/fgets.html

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    8
    thanx....

    that helps heaps..!

    I read the function specs before, but it just wouldnt click!


    thanx again!

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    never use gets()
    forget that its even in the language. Treat it as you would keyword auto and just forget it exists.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    197
    >Treat it as you would keyword auto and just forget it exists.
    Donīt forget it and read automatically fgets() instead.
    Itīs better.

    klausi
    When I close my eyes nobody can see me...

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    8
    I realised that by now.

    I just had some problems with fgets... but it is fine now!

    thanx

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    8

    functions??

    hi.

    I have another trouble now that I cant work out.
    I started using muliple functions in my code and I have a problem with file types.

    I just made a new function, defined like this:

    char checkpal(char in[], int front, int back)

    What it is doing is taking in a string and two integers and, after manipulating the string returning it back to the main function using

    return pal;

    where pal is a variable name for the string to be returned.

    In my main function I called this function using

    var=checkpal(inpt,x,y);

    The problem is that when I try using this new variable 'var' as a string (eg. in printf("%s",var)I keep getting an error
    "format argument not a pointer"
    and if I use '&var' instead of 'var' the output is not exactly what the function checkpal returned. It is my initial input.

    I am not quite clear about the functions.
    I thought that if I put "char checkpal...." that checkpal would return a string which I would easily save under some variable.

    Where am I making a mistake?
    What does the checkpal return to my main function and how do I make a string out of it?


    thank you!


    ---
    I attached the code just in case you need to take a look at it.
    I am just trying to make it work, so thatswhy it is messy and still bad!
    Attached Files Attached Files

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    12

    might work

    Here is my suggestion
    use fgets instead of gets because gets overwrites your memory

    to use fgets
    it takes three arguments

    fgets(stringname,80,stdin);

    try it; it might work

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >What it is doing is taking in a string and two integers and, after manipulating the string returning it back to the main function >using

    >return pal;

    Then you function declaration should be:
    char *checkpal(char in[], int front, int back)

    char is just one character.

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Or this might be easier:

    void checkpal(char in[], int front, int back, char out[])

  11. #11
    Registered User
    Join Date
    Mar 2002
    Posts
    8
    thankx swoopy...

    void checkpal(char in[], int front, int back, char out[])

    is great!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fgets not working after fgetc
    By 1978Corvette in forum C Programming
    Replies: 3
    Last Post: 01-22-2006, 05:33 PM
  2. problem with fgets
    By learninC in forum C Programming
    Replies: 3
    Last Post: 05-19-2005, 08:10 AM
  3. problem with fgets
    By Smoot in forum C Programming
    Replies: 4
    Last Post: 12-07-2003, 02:35 AM
  4. fgets crashing my program
    By EvBladeRunnervE in forum C++ Programming
    Replies: 7
    Last Post: 08-11-2003, 12:08 PM
  5. help with fgets
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-17-2001, 08:18 PM

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