f



How to read an input text from keyboard by the std. lang. C?

Hello,

how to read an input text from keyboard (on a console) by the standard
languace C, please? I really need a tip about this question.

TIA, Vinicius.
0
12/10/2004 6:22:26 PM
comp.lang.c 30656 articles. 5 followers. spinoza1111 (3246) is leader. Post Follow

7 Replies
870 Views

Similar Articles

[PageSpeed] 40

What kind of input? Do you want to read a word? A sentence? A number?
There's more than one way to get input from stdin, but for the most
part you'll find yourself using fgets:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
char buffer[BUFSIZ];

printf("Enter some text: ");
fflush(stdout);
if (fgets(buffer, sizeof buffer, stdin) == NULL) {
fprintf(stderr, "Input error\n");
return EXIT_FAILURE;
}
  printf("%s\n", buffer);

  return EXIT_SUCCESS;
}

-Ren

0
12/10/2004 6:36:49 PM
Vinicius wrote:
> Hello,
> 
> how to read an input text from keyboard (on a console) by the standard
> languace C, please? I really need a tip about this question.
> 
> TIA, Vinicius.

No, you need a clue. A tip would be getchar(). You have a chance of 
getting a clue if you read a decent C reference book.
-- 
Joe Wright                            mailto:joewwright@comcast.net
"Everything should be made as simple as possible, but not simpler."
                     --- Albert Einstein ---
0
joewwright (1738)
12/11/2004 8:25:09 PM
Ever thought of using scanf? What exactly do u want to take as input?
character?string? digit?

dont use "gets" by any way. ur code will crash.

0
afifov (24)
12/12/2004 12:56:53 AM
On Sat, 11 Dec 2004 19:56:53 -0500, "Afifov" <afifov@yahoo.com> wrote:

>Ever thought of using scanf? What exactly do u want to take as input?
>character?string? digit?

Using scanf by itself can be bad advice, since it leaves unparsed input
available for the next input routine.  This wouldn't be a problem for some
people, but has been known to cause infinite loops in programs written by
newcomers. 

>dont use "gets" by any way. ur code will crash.

 gets() will only crash (or otherwise cause undefined behaviour) if the
text entered exceeds the length of the buffer provided. If you want to
discourage the use of gets(), you should point to fgets() as a suitable
substitute.  (In addition, you can also recommend use of the
fgets()/sscanf() combination for parsing input line-by-line.)

0
bk0391 (636)
12/12/2004 5:32:00 AM
Raymond Martineau wrote:
> 
.... snip ...
> 
> >dont use "gets" by any way. ur code will crash.
> 
> gets() will only crash (or otherwise cause undefined behaviour) if
> the text entered exceeds the length of the buffer provided. If you
> want to discourage the use of gets(), you should point to fgets()
> as a suitable substitute.  (In addition, you can also recommend
> use of the fgets()/sscanf() combination for parsing input
> line-by-line.)

Or use the completely safe ggets, with the simplicity of gets, the
safety of fgets, and no worries about input line length.  See it
at:

  <http://cbfalconer.home.att.net/download/ggets.zip>

-- 
Chuck F (cbfalconer@yahoo.com) (cbfalconer@worldnet.att.net)
   Available for consulting/temporary embedded and systems.
   <http://cbfalconer.home.att.net>  USE worldnet address!


0
cbfalconer (19194)
12/12/2004 6:52:02 AM
what does sscanf do?

0
afifov (24)
12/14/2004 12:47:04 PM
On Tue, 14 Dec 2004 07:47:04 -0500, Afifov wrote:

> what does sscanf do?

The same as scanf() and fscanf() except that it reads its input from a
string rather than a file. Any good C reference should explain it.

Lawrence

0
lknews (877)
12/14/2004 1:25:59 PM
Reply: