473,465 Members | 1,929 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

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

Sep 11 '06 #1
11 2701
In article <11*********************@m73g2000cwd.googlegroups. com>,
Alex <ar******@gmail.comwrote:
>The input contains two strings alphanumeric ASCII characters separated
by whitespace. Input is terminated by EOF.
How nice for it.

Sep 11 '06 #2
Alex wrote:
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()?

--
Nick Keighley

Sep 11 '06 #3
Alex wrote:
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.

--
Chris "remember Indiana 3" Dollin
"I'm still here and I'm holding the answers" - Karnataka, /Love and Affection/

Sep 11 '06 #4
Chris Dollin <ch**********@hp.comwrote:
Alex wrote:
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?
Where do you read that?

Richard
Sep 11 '06 #5
Alex wrote:
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
Sep 11 '06 #6
Richard Bos wrote:
Chris Dollin <ch**********@hp.comwrote:
>Alex wrote:
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?

Where do you read that?
Because the spec doesn't appear to allow it:
>>The input contains two strings alphanumeric ASCII characters
separated by whitespace. Input is terminated by EOF."
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.

--
Chris "pleasing coincidence" Dollin
Nit-picking is best done among friends.

Sep 11 '06 #7
Chris Dollin wrote:
Richard Bos wrote:
>Chris Dollin <ch**********@hp.comwrote:
>>Alex wrote:

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?

Where do you read that?

Because the spec doesn't appear to allow it:
>>>The input contains two strings alphanumeric ASCII characters
separated by whitespace. Input is terminated by EOF."

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.

--
Chris "at least it's nearly tea-time" Dollin
The shortcuts are all full of people using them.

Sep 11 '06 #8

Alex wrote:
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!

Sep 11 '06 #9
Konstantin Miller wrote:
Alex wrote:
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");
*strings*, not floating point numbers.

Robert Gamble

Sep 11 '06 #10
kondal wrote:
Alex wrote:
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.

Sep 12 '06 #11
"kondal" <ko******@gmail.comwrites:
kondal wrote:
>Alex wrote:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Sep 12 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Steve Zimmerman | last post by:
This post is not intended as an argument to any other post, just some simple scanf experiments that I wanted to share. I found experiments 5 and 6 the most educational. Also, I thought...
12
by: B Thomas | last post by:
Hi, I was reading O'Reilly's "Practical C programming" book and it warns against the use of scanf, suggesting to avoid using it completely . Instead it recomends to use using fgets and sscanf....
7
by: happy | last post by:
I need to write a string field in a structure data type to a file using fprintf,fscanf. I failed to use the below syntax. Please I need to not go outside scanf,fprintf,printf . /* Book name ...
3
by: linguae | last post by:
Hello. In my C program, I have an array of character pointers. I'm trying to input character strings to each index of the character pointer array using scanf(), but when I run the program, I get...
14
by: iwinux | last post by:
Hi. Before I use scanf(), I must malloc the memory for it, like this: //Start char * buffer; buffer = malloc(20); scanf("%s", &buffer); //End
8
by: john | last post by:
I'm trying to use scanf() to get a string that is terminated by a $sign: Reading a file line that has: account number, name (terminated by $sign) and six numbers: 000001 John Doe$ 4 5 6 7 8 9 ...
20
by: Xavoux | last post by:
Hello all... I can't remind which function to use for safe inputs... gets, fgets, scanf leads to buffer overflow... i compiled that code with gcc version 2.95.2, on windows 2000 char tmp0 =...
3
by: Tinku | last post by:
#include<stdio.h> main() { char line; scanf("%", line); printf("%s", line); } it will read and print the line but what is "%" in general we gives %s, %c .
13
by: AMD | last post by:
Hello, I often need to parse strings which contain a mix of characters, integers and floats, the C-language scanf function is very practical for this purpose. I've been looking for such a...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.