Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Read a Line of Text from the User

hello world fflush

  • Please log in to reply
1 reply to this topic

#1 dcs

dcs

    CC Devotee

  • Just Joined
  • PipPipPipPipPipPip
  • 730 posts

Posted 27 September 2008 - 01:04 PM

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.

[COLOR="Green"]#include [B]<[/B]stdio.h[B]>[/B]
#include [B]<[/B]string.h[B]>[/B][/COLOR]

[COLOR="Blue"]char [/COLOR]*[COLOR="Red"]mygetline[/COLOR]([COLOR="Blue"]char [/COLOR]*line, [COLOR="Blue"]int [/COLOR]size)
{
   [COLOR="Blue"]if [/COLOR]( [COLOR="Red"]fgets[/COLOR](line, size, [COLOR="DarkOrchid"]stdin[/COLOR]) )
   {
      [COLOR="Blue"]char [/COLOR]*newline = [COLOR="Red"]strchr[/COLOR](line, [COLOR="Teal"]'\n'[/COLOR]); [COLOR="Silver"]/* check for trailing '\n' */[/COLOR]
      [COLOR="Blue"]if [/COLOR]( newline )
      {
         *newline =  [COLOR="Teal"]'\0'[/COLOR]; [COLOR="Silver"]/* overwrite the '\n' with a terminating null */[/COLOR]
      }
   }
   [COLOR="Blue"]return [/COLOR]line;
}

[COLOR="Blue"]int [/COLOR][COLOR="Red"]main[/COLOR]([COLOR="Blue"]void[/COLOR])
{
   [COLOR="Blue"]char [/COLOR]text[[COLOR="Teal"]20[/COLOR]] = [COLOR="Teal"]""[/COLOR];
   [COLOR="Red"]fputs[/COLOR]([COLOR="Teal"]"prompt: "[/COLOR], [COLOR="DarkOrchid"]stdout[/COLOR]);
   [COLOR="Red"]fflush[/COLOR]([COLOR="DarkOrchid"]stdout[/COLOR]);
   [COLOR="Red"]printf[/COLOR]([COLOR="Teal"]"text = \"%s\"\n"[/COLOR], [COLOR="Red"]mygetline[/COLOR](text, [COLOR="Blue"]sizeof [/COLOR]text));
   [COLOR="Blue"]return [/COLOR][COLOR="Teal"]0[/COLOR];
}

[COLOR="Silver"]/* my input/output
prompt: hello world
text = "hello world"
*/[/COLOR]

  • 0

#2 dcs

dcs

    CC Devotee

  • Just Joined
  • PipPipPipPipPipPip
  • 730 posts

Posted 27 September 2008 - 01:13 PM

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.

[COLOR="Green"]#include [B]<[/B]stdio.h[B]>[/B][/COLOR]

[COLOR="Blue"]char [/COLOR]*[COLOR="Red"]mygettext[/COLOR]([COLOR="Blue"]char [/COLOR]*text, [COLOR="DarkOrchid"]size_t[/COLOR] size)
{
   [COLOR="DarkOrchid"]size_t [/COLOR]i = [COLOR="Teal"]0[/COLOR];
   for ( ;; )
   {
      [COLOR="Blue"]int [/COLOR]ch = [COLOR="Red"]fgetc[/COLOR]([COLOR="DarkOrchid"]stdin[/COLOR]);
      [COLOR="Blue"]if [/COLOR]( ch == [COLOR="Teal"]'\n'[/COLOR] || ch == [COLOR="DarkOrchid"]EOF [/COLOR])
      {
         [COLOR="Blue"]break[/COLOR];
      }
      [COLOR="Blue"]if [/COLOR]( i < size - [COLOR="Teal"]1[/COLOR] )
      {
         text[i++] = ch;
      }
   }
   text[i] = [COLOR="Teal"]'\0'[/COLOR];
   [COLOR="Blue"]return [/COLOR]text;
}

[COLOR="Blue"]int [/COLOR][COLOR="Red"]main[/COLOR]([COLOR="Blue"]void[/COLOR])
{
   [COLOR="Blue"]int [/COLOR]i;
   [COLOR="Blue"]for [/COLOR]( i = [COLOR="Teal"]0[/COLOR]; i < [COLOR="Teal"]3[/COLOR]; ++i )
   {
      [COLOR="Blue"]char [/COLOR]text[[COLOR="Teal"]20[/COLOR]];
      [COLOR="Red"]fputs[/COLOR]([COLOR="Teal"]"prompt: "[/COLOR], [COLOR="DarkOrchid"]stdout[/COLOR]);
      [COLOR="Red"]fflush[/COLOR]([COLOR="DarkOrchid"]stdout[/COLOR]);
      [COLOR="Red"]printf[/COLOR]([COLOR="Teal"]"text = \"%s\"\n"[/COLOR], [COLOR="Red"]mygettext[/COLOR](text, [COLOR="Blue"]sizeof [/COLOR]text));
   }
   [COLOR="Blue"]return [/COLOR][COLOR="Teal"]0[/COLOR];
}

[COLOR="Silver"]/* my input/output
prompt: 1234567890123456789012345
text = "1234567890123456789"
prompt: hello world
text = "hello world"
prompt: goodbye
text = "goodbye"
*/[/COLOR]

  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download