Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. ansi c: gets dangerous

ansi c: gets dangerous

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kfaday
    wrote on last edited by
    #1

    Hi, i'm doing an ansi c program, compiling with gcc. I get a warning that the gets function is dangerous. Everything works fine, but i don't want to get that warning.

    char *Comando=0;
    Comando=(char*)malloc(150*sizeof(char));

    gets(Comando);

    I can't (or don't know how to) use scanf as if i type a string with spaces scanf only saves the characters until the first space. which function could i use apart from gets (and it must be ansi c )? thanks!

    S H J 3 Replies Last reply
    0
    • K kfaday

      Hi, i'm doing an ansi c program, compiling with gcc. I get a warning that the gets function is dangerous. Everything works fine, but i don't want to get that warning.

      char *Comando=0;
      Comando=(char*)malloc(150*sizeof(char));

      gets(Comando);

      I can't (or don't know how to) use scanf as if i type a string with spaces scanf only saves the characters until the first space. which function could i use apart from gets (and it must be ansi c )? thanks!

      S Offline
      S Offline
      Stefan Pedersen
      wrote on last edited by
      #2

      The problem with gets() is that is doesn't consider the size of the destination buffer. In your case just enter more that 150 characters and PRESTO! Insta-crash(tm). May I recomand fgets() instead? And if the paths that I have followed/have tread against the flow/there is no need for sorrow I am coming home Return, Crüxshadows

      1 Reply Last reply
      0
      • K kfaday

        Hi, i'm doing an ansi c program, compiling with gcc. I get a warning that the gets function is dangerous. Everything works fine, but i don't want to get that warning.

        char *Comando=0;
        Comando=(char*)malloc(150*sizeof(char));

        gets(Comando);

        I can't (or don't know how to) use scanf as if i type a string with spaces scanf only saves the characters until the first space. which function could i use apart from gets (and it must be ansi c )? thanks!

        H Offline
        H Offline
        Henry miller
        wrote on last edited by
        #3

        That warning is telling you that it is IMPOSSIBLE to use gets correctly! If your program uses gets I can crash it. Any professor who sees gets in code should automaticly give the student an F! instead you should use fgets, which can replace your use of gets like this: fgets(Comando,150,STDIN); See, it is easy to use fgets, and I can no longer crash your program just be doing something stupid.

        K 1 Reply Last reply
        0
        • K kfaday

          Hi, i'm doing an ansi c program, compiling with gcc. I get a warning that the gets function is dangerous. Everything works fine, but i don't want to get that warning.

          char *Comando=0;
          Comando=(char*)malloc(150*sizeof(char));

          gets(Comando);

          I can't (or don't know how to) use scanf as if i type a string with spaces scanf only saves the characters until the first space. which function could i use apart from gets (and it must be ansi c )? thanks!

          J Offline
          J Offline
          James R Twine
          wrote on last edited by
          #4

          There is an extension to the scanf(...) function that allows to scan non-space-delimited strings.  I do not know if it is available in GCC's libraries...    It works like this: to scan a string that is terminated by a semicolon, your format specifier would be: %[^;]. For example, if you wanted to scan this string: C:12345;B:09876    And extract the two numeric values into two strings (12345 and 09876), you would write something like:     char caStringBuf1[ 64 + 1 ];     char caStringBuf2[ 64 + 1 ];     sscanf( "C:12345;B:09876","**C:%[^;];B:%s**",           caStringBuf1, caStringBuf2 );     caStringBuf1[ 64 ] = '\0';     caStringBuf2[ 64 ] = '\0';    You would want to limit the input test somehow, of course and add some validation code, but that would scan the string correctly.    Peace! -=- James Tip for inexperienced drivers: "Professional Driver on Closed Course" does not mean "your Dumb Ass on a Public Road"!
          Articles -- Products: Delete FXP Files & Check Favorites

          1 Reply Last reply
          0
          • H Henry miller

            That warning is telling you that it is IMPOSSIBLE to use gets correctly! If your program uses gets I can crash it. Any professor who sees gets in code should automaticly give the student an F! instead you should use fgets, which can replace your use of gets like this: fgets(Comando,150,STDIN); See, it is easy to use fgets, and I can no longer crash your program just be doing something stupid.

            K Offline
            K Offline
            kfaday
            wrote on last edited by
            #5

            thanks, i used fgets

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups