Thread: Why GCC does warn me when I using gets() function for accessing file?

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    10

    Question Why GCC does warn me when I using gets() function for accessing file?

    After compiling the source code with gcc v.4.1.1, I got a warning message:
    "/tmp/ccixzSIL.o: In function 'main';ex.c: (.text+0x9a): warning: the 'gets' function is dangerous and should not be used."

    Could anybody tell me why gets() function is dangerous??
    Thank you very much.

    Cuthbert

    Here is the source code I was testing:
    Code:
    ---------------------------------------------------
    * count.c -- using standard I/O */
    
    #include "stdafx.h"
    #include <stdio.h>
    #include <stdlib.h> // ANSI C exit() prototype
    
    int main(int argc, char *argv[])
    {
        int ch;         // place to store each character as read
        FILE *fp;       // "file pointer" 
        long count = 0;
    
        if (argc != 2)
        {
            printf("Usage: %s filename\n", argv[0]);
            exit(1);
        }
        if ((fp = fopen(argv[1], "r")) == NULL)
        {
            printf("Can't open %s\n", argv[1]);
            exit(1);
        }
        while ((ch = getc(fp)) != EOF)
        {
           putc(ch,stdout);  // same as putchar(ch);
           count++;
        }
        fclose(fp);
        printf("File %s has %ld characters\n", argv[1], count);
        
        return 0;
    }
    ------------------------------------------------------------------
    Last edited by Ken Fitlike; 09-03-2006 at 01:13 PM. Reason: code tags added

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by cuthbert
    Could anybody tell me why gets() function is dangerous??
    With simple words. No matter how large you make the buffer, the user just has to input one character more and boooom.
    Kurt

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Read FAQ for a proper reason
    Why gets() is wrong


    ssharish2005
    Last edited by ssharish2005; 09-03-2006 at 01:38 PM.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    And what to use instead, fgets(): http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Of course it could be warning you because you don't actually use it for files too.


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

  6. #6
    and Nothing Else Matters
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    117
    Perhaps i've gone blind but i cant see any gets() calls in the code you just posted cuthbert
    It is not who I am inside but what I do that defines me.

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    ...maybe Ken took it off too? Bad programming habits should be removed at any cost
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  8. #8
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Failure to do even one simple google search for the answer always astounds me: http://www.google.com/search?hl=en&l...tion+dangerous
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM