Like Tree1Likes

fgets not truncating text

This is a discussion on fgets not truncating text within the C Programming forums, part of the General Programming Boards category; Personally, I would initialise read_ok to 0 and return_val to NULL, even though they are correctly given initial values within ...

  1. #31
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,650
    Personally, I would initialise read_ok to 0 and return_val to NULL, even though they are correctly given initial values within the loop body. prompt should be a const char*. I would prefer strchr to strstr here. (Nominal Animal's strcspn idea does not fit quite as well into your current logic, methinks.) Oh, and all those extra blank lines make me
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #32
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,344
    Quote Originally Posted by laserlight View Post
    Personally, I would initialise read_ok to 0 and return_val to NULL, even though they are correctly given initial values within the loop body. prompt should be a const char*. I would prefer strchr to strstr here. (Nominal Animal's strcspn idea does not fit quite as well into your current logic, methinks.) Oh, and all those extra blank lines make me
    Initialising is always a good idea.

    strchr is a better idea

    const char * is a better idea

    I don't know where you are seeing Nominal Animal's suggestion, though...

    The extra blank lines are from a A-Style indentation program -> I'm fairly sure that I'm not a fan of it either...
    Fact - Beethoven wrote his first symphony in C

  3. #33
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,344
    Here is one that the whole family can enjoy...
    Code:
    char *get_user_input (const char *prompt, char *input_string, size_t input_string_size);
    
    
    int main(void)
    {
        char *fname;
    
    
        if ((fname = malloc(MAXNAME * sizeof(*fname))) == NULL)
        {
            fprintf(stderr, "-ERROR-\nAllocation Failure\n");
            return EXIT_FAILURE;
        }
    
    
        if (get_user_input ("Enter first name", fname, MAXNAME) != NULL)
        {
            printf("Name is %s\n", fname);
        }
        else
        {
            fprintf(stderr, "-ERROR-\nProblem with reading string\n");
            return EXIT_FAILURE;
        }
    
    
        free(fname);
    
    
        return EXIT_SUCCESS;
    }
    
    
    char *get_user_input (const char *prompt, char *input_string, size_t input_string_size)
    {
        int read_ok = 0;
        char *return_val = NULL;
    
    
        if (prompt == NULL || input_string == NULL || input_string_size <= 0)
        {
            return NULL;
        }
    
    
        do
        {
            char *s;
    
    
            printf("%s: ", prompt);
            fflush(stdout);
    
    
            if ((return_val = fgets(input_string, input_string_size, stdin)) == NULL)
            {
                break;
            }
    
    
            if ((s = strchr(input_string, '\n')) != NULL)
            {
                *s = '\0';
                read_ok = 1;
            }
            else
            {
                int c;
                while((c = getchar()) != '\n' && c != EOF)
                {
                    continue;
                }
    
    
                fprintf(stderr, "Problem with string size\n");
                read_ok = 0;
            }
        }
        while (read_ok == 0);
    
    
        return return_val;
    }
    Fact - Beethoven wrote his first symphony in C

  4. #34
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,650
    Quote Originally Posted by Click_here
    I don't know where you are seeing Nominal Animal's suggestion, though...
    In a different thread, which you may have seen since you've been around long enough.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #35
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,344
    Quote Originally Posted by laserlight View Post
    In a different thread, which you may have seen since you've been around long enough.
    I think I found what you were talking about here - Scanning new line - I remember it now; how can I forget this classic by soma

    [Edit]
    *sigh*


    Removed the wrong portion of the post.


    I have no idea what was written here. Sorry.
    [/Edit]
    Nominal Animal's code is a very neat way of removing new line characters. However, you are right in saying that it would not fit my example, as the problem I tried to solve is whether a new line has been included in the input from fgets
    Fact - Beethoven wrote his first symphony in C

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. FGETS from text to array
    By Oonej in forum C Programming
    Replies: 30
    Last Post: 07-10-2011, 01:46 AM
  2. Fgets() & Fputs() to combine text files?
    By d2rover in forum C Programming
    Replies: 5
    Last Post: 11-20-2010, 02:58 PM
  3. truncating front
    By jimmianlin in forum C Programming
    Replies: 2
    Last Post: 10-01-2009, 04:12 PM
  4. Truncating a char *
    By ajb268 in forum C++ Programming
    Replies: 3
    Last Post: 01-31-2005, 01:57 PM
  5. Truncating a double?
    By criticalerror in forum C++ Programming
    Replies: 13
    Last Post: 12-08-2003, 11:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21