Thread: fgets() and strlen() confusion

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You can use scanf() super safely but it can be a pain in the ass.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main()
    {
        char *data = NULL;
        char format[16];
        int length = 0;
        int convert = 0;
        puts("Please enter a string length.");
        convert = scanf("%i", &length);
        if (convert == 1 && length > 1)
        {
            data = malloc(1 + length);
            sprintf(format, "%%%ds", length);
        }
        else
        {
            fputs("Bad string length entered.\n",stderr);
            return 1;
        }
    
        printf("OK, enter your string (length = %d).\n", length);
        convert = scanf(format, data);
        if (convert == 1)
        {
            printf("Good job.\n\"%s\"\n", data);
        }
        else
        {
            fputs("There was a problem.\n",stderr);
        }
        free(data);
        data = NULL;
    
        return 0;
    }
    
    /*
    Please enter a string length.
    20
    OK, enter your string (length = 20).
    Low-calorie.beer
    Good job.
    "Low-calorie.beer"
    */
    Basically you need to do that. I guess it could look worse, and I didn't check malloc(). Oh well. There's annoying things about scanf that won't be fixed, like not reading strings longer than one word.

    [edit] It's not that scanf is a bad function that works poorly, but user input is frequently not formatted at all. Use scanf to read formatted files or something. It can be a lot of work to shoehorn this one way you know to get input into the program securely.[/edit]
    Last edited by whiteflags; 09-25-2012 at 06:15 PM. Reason: So it turns out laserlight mentioned this...

  2. #2
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    956
    Quote Originally Posted by whiteflags View Post
    Basically you need to do that. I guess it could look worse, and I didn't check malloc(). Oh well. There's annoying things about scanf that won't be fixed, like not reading strings longer than one word.
    Yes, it is annoying to create a format string for scanf, but scanf does have a way to read arbitrary strings, including whitespace, using the '[' format specifier. A common format is "%[\n]" (or something like "%20[\n]" with a maximum length).

    I do prefer using fgets, though, since users in general expect the program to read whole lines at a time. sscanf can then be used on each input line string if needed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strlen
    By _arjun in forum C Programming
    Replies: 9
    Last Post: 06-10-2012, 11:20 AM
  2. strlen
    By salvadoravi in forum C Programming
    Replies: 15
    Last Post: 01-11-2008, 05:43 PM
  3. strlen()
    By exoeight in forum C Programming
    Replies: 9
    Last Post: 04-01-2005, 10:18 AM
  4. Strlen(...)
    By Korhedron in forum C++ Programming
    Replies: 6
    Last Post: 06-10-2003, 03:02 PM
  5. Why O why, strlen?
    By Sebastiani in forum C Programming
    Replies: 11
    Last Post: 08-24-2001, 01:41 PM