+ Reply to Thread
Results 1 to 2 of 2

Thread: Read a Line of Text from the User

  1. #1
    dcs
    dcs is offline Guru
    Join Date
    Mar 2008
    Posts
    775
    Rep Power
    23

    Read a Line of Text from the User

    Read a Line of Text from the User

    Obtaining user input can be done in many surprisingly different ways. This code is somewhere in the middle: safer than gets(text) or scanf("%s", text), but not bulletproof. It is meant to be a simple but relatively safe demonstration.

    The function mygetline reads user input from the stdin into a string using fgets. Then it attempts to remove a trailing newline that fgets may leave in the string. It returns a pointer to the destination string.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    char *mygetline(char *line, int size)
    {
       if ( fgets(line, size, stdin) )
       {
          char *newline = strchr(line, '\n'); /* check for trailing '\n' */
          if ( newline )
          {
             *newline =  '\0'; /* overwrite the '\n' with a terminating null */
          }
       }
       return line;
    }
    
    int main(void)
    {
       char text[20] = "";
       fputs("prompt: ", stdout);
       fflush(stdout);
       printf("text = \"%s\"\n", mygetline(text, sizeof text));
       return 0;
    }
    
    /* my input/output
    prompt: hello world
    text = "hello world"
    */

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    dcs
    dcs is offline Guru
    Join Date
    Mar 2008
    Posts
    775
    Rep Power
    23

    Re: CodeCall Contest #6

    Read a Line of Text from the User, Discard Excess Characters

    If the user tries to put 80 characters in a 20-character buffer, you may have issues. This snippet shows one way to cap the input and discard excess input.

    Code:
    #include <stdio.h>
    
    char *mygettext(char *text, size_t size)
    {
       size_t i = 0;
       for ( ;; )
       {
          int ch = fgetc(stdin);
          if ( ch == '\n' || ch == EOF )
          {
             break;
          }
          if ( i < size - 1 )
          {
             text[i++] = ch;
          }
       }
       text[i] = '\0';
       return text;
    }
    
    int main(void)
    {
       int i;
       for ( i = 0; i < 3; ++i )
       {
          char text[20];
          fputs("prompt: ", stdout);
          fflush(stdout);
          printf("text = \"%s\"\n", mygettext(text, sizeof text));
       }
       return 0;
    }
    
    /* my input/output
    prompt: 1234567890123456789012345
    text = "1234567890123456789"
    prompt: hello world
    text = "hello world"
    prompt: goodbye
    text = "goodbye"
    */

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. How to read all next line in file?
    By Hamed in forum C and C++
    Replies: 3
    Last Post: 11-04-2010, 02:21 PM
  2. Read user input with bash?
    By Crop in forum Linux Programming and Scripting
    Replies: 4
    Last Post: 08-29-2009, 09:21 AM
  3. Read a Floating-Point Value from the User
    By dcs in forum C Tutorials
    Replies: 2
    Last Post: 09-27-2008, 03:08 PM
  4. Read an Integer from the User
    By dcs in forum C Tutorials
    Replies: 2
    Last Post: 09-27-2008, 02:46 PM
  5. Read particular line in txt document
    By Danerd100 in forum Visual Basic Programming
    Replies: 5
    Last Post: 05-29-2008, 01:10 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts