Thread: how to use fgets noob

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    96

    how to use fgets noob

    i wanted to know how to use fgets(); to asked the user for a string. instead of using scanf to scan for a string instead becasue scanf can cause buffer overflow.
    can someone give me an example



    for examaple
    Code:
    #include <stdio.h>
    
    
    main()
    {
          
          char name[25];
          printf("name is");
          scanf("%s",&name);     // this is  not good because it can cause overflow if my  string is larger thatn 25. so how do you use fgets  
          
          
          
    }

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    fgets(name,25,stdin);
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    96
    what the hell is stdn

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It's kinda like stdin, except it doesn't have an i.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    96
    i mean what is stdin. can someone one tell me please. i want to get better at programing

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You should discover the joys of a search engine. In the couple of minutes between posts, you didn't think to type 'stdin' in a search engine? Or even into the search field of this forum?


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Feb 2010
    Posts
    96
    that is what a forum is for ask question

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    A forum is not a replacement for a search engine and you using what I assume is contained in your cranium...a brain?

  9. #9
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by mouse666666 View Post
    that is what a forum is for ask question
    That's true but you could at least imagine that this is a question that has been asked hundreds of thousands of times, and you COULD do a search for stdin.

    Let me google that for you
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    He's probably got it in a C book some place, and just didn't bother looking it up.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User jephthah's Avatar
    Join Date
    May 2010
    Location
    seattle
    Posts
    49
    FTR,

    you can still overflow the stdin buffer with fgets()... won't be a spectacular crash, but you'll likely lose input data and make your program all wonky.

    likewise, you can make scanf() protect the input variable from having an overflow.

    just sayin'

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by jephthah View Post
    you can still overflow the stdin buffer with fgets()... won't be a spectacular crash, but you'll likely lose input data and make your program all wonky.
    You have no idea what you're talking about, or at least how to correctly word what it is you think you're talking about.


    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Registered User jephthah's Avatar
    Join Date
    May 2010
    Location
    seattle
    Posts
    49
    its late here, im not expressing clearly:

    fgets, if you dont size your char buffer properly , will leave uncollected input in the input stream. i shouldnt have said overflow. but the uncollected input will (unless the routine is sufficiently error-checked) will most likely cause the operation of the program to fail.

    scanf if formatted correctly, can protect the char buffer from being overflowed.

    all i'm saying is that scanf is not Teh Evil, and fgets is not Teh Messiah. though for new users, its probably best if they stay away from scanf

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