Thread: fgets and gets

  1. #1
    Registered User
    Join Date
    Apr 2006

    fgets and gets

    well, the function gets(), reads char until the user press <enter>, right?
    the function fgets(), reads char from a file until apear '\n' or '\0'?
    plz explain me this

  2. #2
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Whats to explain? fgets can read in from the keyboard as well - as
    in it will read in until is reads an enter key (or the buffer its writing
    to gets full). The '\n' character is the same as pressing enter - the
    computer sees enter as an ASCII character. As for '\0' thats the
    string termination character - it won't cause fgets to stop reading
    I think.
    No No's:
    fflush (stdin); gets (); void main ();

    Example of fgets (); The FAQ, C/C++ Reference

    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition

    ASCII stupid question, get a stupid ANSI

  3. #3
    Registered User
    Join Date
    Feb 2006
    Some of you know what buffer expliots are...
    well....gets is the buggy one that is unsafe....even the compiler tells you i think...
    and fgets...or something like that is a revised version...

    In gets, you specified the buffer length.....
    So lets say you said you got a size of 12....
    ANSI is encoded in basically, a character per a byte....
    So you have a 12 byte buffer, + 4 bytes for prog stack ops, and then return address from call.
    SO, using gets, you can convert assembly code instructions, and a return address to execute, in fact, the string in gets as assembly code.....
    You can compile your assembly into object code and then dump it to get hex instructions, and then just tag on the return address(the address of gets in the buffer which can be found using GDB) and the program will execute you exploit code...
    Basically, thats how a hack is made....
    If your really good, you'll patch the stack during your exploit so the prog doesnt even know it did something wrong and wont segfault.....
    fgets fixes that....or some other gets function....i dont remember.....c was a while back...

  4. #4
    Registered User
    Join Date
    Apr 2006
    Fgets() and gets() are two different functions. Gets() is never a macro like printf() can be.

    Quote Originally Posted by iRMX® C Library Reference, on gets
    The line consists of all characters up to and including the first newline character \n.
    The gets( ) function replaces the newline character with a null character \0 before
    returning the line.
    The fgets( ) function retains the newline character.
    fgets() also takes an aditional length paramitter. Prototypes:
    char *fgets (char *string, int n, FILE *stream);
    char *gets (char *buffer);

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    >plz explain me this
    It's very simple, really. gets is a figment of your imagination. fgets is what we use to read a line from a stream.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed