Thread: fgets not truncating text

  1. #31
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,907
    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,907
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,907
    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

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, 03: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, 02:57 PM
  5. Truncating a double?
    By criticalerror in forum C++ Programming
    Replies: 13
    Last Post: 12-08-2003, 12:49 PM