Thread: fgets

  1. #16
    Code
    Guest
    And a ; after char name[20];<----

  2. #17
    Cody
    Guest
    sorry, and a #include <string.h>

    sorry, I can't edit because I haven't registered yet.

  3. #18
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >> didn't read all the posts above, but your code should look like this
    No, it shouldn't
    You don't need sscanf(), and you forget a semi-colon here:
    >char name[20]
    and you didn't check the return code from fgets().
    [edit] I see you corrected yourself.

    No offence, but the topic has really been covered already, if the original poster is still having problems, I suggest they post some code for us to review.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #19
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    The gets(char*) standard library function is bad because there is no built in protection to regulate the size of the input stream. The input stream can therefore extend beyond the size of the array you are using to store the input, and consequently overwrite the reserved memory of the string. This will crash your program.

    Where gets() reads until the first newline character, fscanf(...) reads until it encounters a white space character in the input stream. It is more difficult to overwrite an array with fscanf, however it is still possilbe. The problem with fscanf is that it is awkward to control. I've had some scope problems when working with it. It does not seem to be able to control a loop effectively.

    At any rate fgets can be used in the stead of either of the above, mainly because it has size safetly built into it, but there are other reasons for it being generally more useful.

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, 06: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, 03: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