stdin

G

Guest

Can I append a string to stdin? How?

How can I read from stdin a whole line?
because scanf("%s", string) reads only a word even if I use ""

thanks
 
A

Al Bowers

Can I append a string to stdin? How?

How can I read from stdin a whole line?
because scanf("%s", string) reads only a word even if I use ""

Usually it's far easier to read entire lines (with fgets or the like),
then interpret them, either using sscanf or some other techniques.

You should read the faq, specially section 12 on stdin and specifically
Question 12.20
Why does everyone say not to use scanf? What should I use instead?
http://www.eskimo.com/~scs/C-faq/q12.20.html
 
P

pete

Richard said:
"Append"? What do you mean by "appending" a string to stdin? It's an
input file, you can't write things to it.

Terminolgy:

stdin is an expression of type, pointer to FILE.
That FILE type object, controls the standard input stream.

A "file" is something on the opposite end of a stream,
from your computer, like a keyboard or a disk file,
but not stdin.

Both K&R and the C standard,
sometimes refer to the standard input stream as "stdin",
though I wish that they didn't.
 
E

Emmanuel Delahaye

<- Chameleon -> said:
Can I append a string to stdin? How?

How can I read from stdin a whole line?
because scanf("%s", string) reads only a word even if I use ""

fgets() is the correct function to get a line of text.
 
D

Dan Pop

In said:
fgets() is the correct function to get a line of text.

scanf is not only as "correct" for this job, it is also MUCH easier to
use (automatic elimination of the newline character, automatic truncation
of longer lines).

Dan
 
M

Malcolm

Dan Pop said:
scanf is not only as "correct" for this job, it is also MUCH easier to
use (automatic elimination of the newline character, automatic truncation
of longer lines).
scanf() can do wonderful things. The problem is you have to master its
format string. Furthermore, you have to know that the other programmers on
your team have also mastered its intricacies.
 
D

Dan Pop

scanf() can do wonderful things. The problem is you have to master its
format string.

A 15 minute job for any C programmer. And a piece of cake for anyone
who has already mastered the Fortran FORMAT statements.
Furthermore, you have to know that the other programmers on
your team have also mastered its intricacies.

Why? If they don't understand my code, they know where to find me.

The scan set required by such a scanf() call is really trivial.
A little less trivial is how to decide whether to call getchar() after
or not.

char buff[1024 + 1];
int rc = scanf("%1024[^\n]%*[^\n]", buff);
if (!feof(stdin) && !ferror(stdin)) getchar();

If rc == 0 stdin was already at eof and the contents of buff should not
be examined at all.

Dan
 
D

Dan Pop

In said:
scanf() can do wonderful things. The problem is you have to master its
format string.

A 15 minute job for any C programmer. And a piece of cake for anyone
who has already mastered the Fortran FORMAT statements.
Furthermore, you have to know that the other programmers on
your team have also mastered its intricacies.

Why? If they don't understand my code, they know where to find me.

The scan set required by such a scanf() call is really trivial.
A little less trivial is how to decide whether to call getchar() after
or not.

char buff[1024 + 1];
int rc = scanf("%1024[^\n]%*[^\n]", buff);
if (!feof(stdin) && !ferror(stdin)) getchar();

If rc == 0 stdin was already at eof and the contents of buff should not
be examined at all.

Sorry for leaving my brain in neutral when writing the last sentence.

If rc == EOF stdin was already at eof (or encountered an eof condition
while trying to get the first character).

rc == 0 when the user enters an empty line. To avoid treating this as
a special case, initialise buff to an empty string before calling scanf.

Dan
 

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
473,776
Messages
2,569,603
Members
45,190
Latest member
Martindap

Latest Threads

Top