How to use scanf() for input 2 strings ended of EOF?

A

Alex

The input contains two strings alphanumeric ASCII characters separated
by whitespace. Input is terminated by EOF.
 
N

Nick Keighley

Alex said:
The input contains two strings alphanumeric ASCII characters separated
by whitespace. Input is terminated by EOF.

maybe you want to look at fgets() and sscanf()?
 
C

Chris Dollin

Alex said:
The input contains two strings alphanumeric ASCII characters separated
by whitespace. Input is terminated by EOF.

(If EOF didn't terminate it, it wouldn't be called *E*OF; it seems a bit
redundant to say that.)

So the input is required to have no line-end characters? That seems ...
unwise.
 
R

Richard Bos

Chris Dollin said:
(If EOF didn't terminate it, it wouldn't be called *E*OF; it seems a bit
redundant to say that.)

So the input is required to have no line-end characters?

Where do you read that?

Richard
 
C

Chris Dollin

Richard said:
Where do you read that?

Because the spec doesn't appear to allow it:

There's nothing in there that allows whitespace (of which end-of-line,
aka \n, is an instance) after the second alphanumeric string.

It's probably a buggy specification, although some people might take
the position that it's a picky reading.
 
C

Chris Dollin

Chris said:
Because the spec doesn't appear to allow it:


There's nothing in there that allows whitespace (of which end-of-line,
aka \n, is an instance) after the second alphanumeric string.

It's probably a buggy specification, although some people might take
the position that it's a picky reading.

.... and I see that of course it allows end-of-lines /between/ the
tokens. Just not at the end.

A sloppy picky reading, then.

Blame it on the latte's confounded absence.
 
K

Konstantin Miller

Alex said:
The input contains two strings alphanumeric ASCII characters separated
by whitespace. Input is terminated by EOF.

man sscanf or google sscanf

double a, b;
if( 2 != sscanf(input, "%lf %lf", &a, &b) ) printf("error\n");

Konstantin
 
K

kondal

Alex said:
The input contains two strings alphanumeric ASCII characters separated
by whitespace. Input is terminated by EOF.

char buf[1000]="";
int len = 1000; /*length of the strings combined. */
fread(buf, len, 1, stdin);

Then use strtok to get the individual strings.

Hope this works!
 
R

Robert Gamble

Konstantin said:
man sscanf or google sscanf

double a, b;
if( 2 != sscanf(input, "%lf %lf", &a, &b) ) printf("error\n");

*strings*, not floating point numbers.

Robert Gamble
 
K

kondal

kondal said:
Alex said:
The input contains two strings alphanumeric ASCII characters separated
by whitespace. Input is terminated by EOF.

char buf[1000]="";
int len = 1000; /*length of the strings combined. */
fread(buf, len, 1, stdin);

Then use strtok to get the individual strings.

Hope this works!

if the length of the strings isn't known, you can read and fill the
buffer till eof is reached using feof function.
 
K

Keith Thompson

kondal said:
kondal said:
Alex said:
The input contains two strings alphanumeric ASCII characters separated
by whitespace. Input is terminated by EOF.

char buf[1000]="";
int len = 1000; /*length of the strings combined. */
fread(buf, len, 1, stdin);

Then use strtok to get the individual strings.

Hope this works!

if the length of the strings isn't known, you can read and fill the
buffer till eof is reached using feof function.

feof() probably isn't what you want. fread() returns the number of
items succesfully read; if its return value is less than the number of
items requested, you can *then* use feof() to find out whether it was
because you reached the end of the file or because of an error.
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top