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

V

Vinicius

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.
 
R

Ren

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
 
J

Joe Wright

Vinicius said:
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.
 
A

Afifov

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.
 
R

Raymond Martineau

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.)
 
C

CBFalconer

Raymond said:
.... snip ...


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>
 
L

Lawrence Kirby

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,156
Messages
2,570,878
Members
47,408
Latest member
AlenaRay88

Latest Threads

Top