Thread: difference between gets and fgets here???

  1. #1
    Registered User n3cr0_l0rd's Avatar
    Join Date
    Feb 2009
    Posts
    62

    difference between gets and fgets here???

    Code:
    #include <stdio.h>
    #include <conio.h>
    int main()
    {
        char s[100];
        int i=0;
        //fgets(s,100,stdin);
        gets(s);
        while ( s[i] != '\0' ) i++;
        printf("%d",i);
        getch();
        return 0;
    }

    when i use gets the output is same as number of characters in the string but using fgets gives value more by 1... example: input is "Kiran".. using gets gives output 5 but using fgets, it gives 6.
    I had crush on her, before that crush could CRUSH me, i CRUSHED that crush !!! Fun time over, Lets find a way to not to blow off the whole leg !

    __________________________________________________ __________________________________________________ _______________

    In C++ it's harder to shoot yourself in the foot, but when you do, you blow off your whole leg.
    - Bjarne Stroustrup

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    fgets stores the newline in the buffer, as well. If you don't want it, you must strip it (ie delete it).
    For that matter, I hope you know that gets is bad.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User n3cr0_l0rd's Avatar
    Join Date
    Feb 2009
    Posts
    62
    Quote Originally Posted by Elysia
    delete it
    how ?
    i know using gets is bad so i wanted to use fgets...
    I had crush on her, before that crush could CRUSH me, i CRUSHED that crush !!! Fun time over, Lets find a way to not to blow off the whole leg !

    __________________________________________________ __________________________________________________ _______________

    In C++ it's harder to shoot yourself in the foot, but when you do, you blow off your whole leg.
    - Bjarne Stroustrup

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by n3cr0_l0rd View Post
    how ?
    i know using gets is bad so i wanted to use fgets...
    FAQ > How do I... (Level 1) > Get a line of text from the user/keyboard (C)
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    Or, alternatively, here.

    Summary: fgets() keeps the newline, gets() does not. fgets() protects against the user entering more characters than the string is long, gets() does not.
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

Popular pages Recent additions subscribe to a feed