473,503 Members | 1,747 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

can fscanf skip reading data conditionally?

I need to read data from the file like the following with name and
score, but some line may only has name without score:

joe 100
amy 80
may

Here's my code, but it couldn't read the line with "may" because there
is no score. Anyone knows what is the workaround to this problem?

char name[20];
int score;

while (fscanf(ifp, "%s %d", name, &score) == 2)
{
printf("%s %d\n", name, score);
}
please advice. thanks!!

Sep 27 '06 #1
4 4210
John wrote:
I need to read data from the file like the following with name and
score, but some line may only has name without score:

joe 100
amy 80
may

Here's my code, but it couldn't read the line with "may" because there
is no score. Anyone knows what is the workaround to this problem?

char name[20];
int score;

while (fscanf(ifp, "%s %d", name, &score) == 2)
{
printf("%s %d\n", name, score);
}
int i;
while ((i = fscanf(ifp, "%s %d", name, &score)) >= 1)
{
if (i == 2)
printf("%s %d\n", name, score);
else
printf("%s\n", name);
}

Robert Gamble

Sep 27 '06 #2
John wrote:
I need to read data from the file like the following with name and
score, but some line may only has name without score:

joe 100
amy 80
may

Here's my code, but it couldn't read the line with "may" because there
is no score. Anyone knows what is the workaround to this problem?

char name[20];
int score;

while (fscanf(ifp, "%s %d", name, &score) == 2)
{
printf("%s %d\n", name, score);
}
You did not specify what you want to do if you encounter this problem.
One example:

char name[20];
int score;
int ret;

while (EOF != (ret = fscanf(ifp, "%19s %d", name, &score)))
{
if (ret 0) {
printf("%s", name);
if (ret 1) {
printf("%d", score);
}
}
putchar('\n');
}

Next time, explain what you want to do, what you have, how the
results of your code do not match what you expected, and give
a compilable minimal example. This helps us help you.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Sep 27 '06 #3

"John" <ja*****@gmail.comwrote in message
news:11*********************@i3g2000cwc.googlegrou ps.com...
>I need to read data from the file like the following with name and
score, but some line may only has name without score:

joe 100
amy 80
may

Here's my code, but it couldn't read the line with "may" because there
is no score. Anyone knows what is the workaround to this problem?

char name[20];
int score;

while (fscanf(ifp, "%s %d", name, &score) == 2)
{
printf("%s %d\n", name, score);
}
What makes you think it couldn't read the line?
I'll bet it did it just fine, and returned 1.
>
please advice. thanks!!
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project
Sep 27 '06 #4
"Robert Gamble" <rg*******@gmail.comwrites:
John wrote:
>I need to read data from the file like the following with name and
score, but some line may only has name without score:

joe 100
amy 80
may

Here's my code, but it couldn't read the line with "may" because there
is no score. Anyone knows what is the workaround to this problem?

char name[20];
int score;

while (fscanf(ifp, "%s %d", name, &score) == 2)
{
printf("%s %d\n", name, score);
}

int i;
while ((i = fscanf(ifp, "%s %d", name, &score)) >= 1)
{
if (i == 2)
printf("%s %d\n", name, score);
else
printf("%s\n", name);
}
fscanf() skips whitespace, including newlines. If you call fscanf
with a format of "%s %d", it will read a blank delimited word, then
look for a decimal integer; if there isn't one on the current line, it
will continue reading lines until it finds either a decimal integer or
something that definitely isn't one.

That might happen to work, but it's fragile. Also, it won't detect
an error such as:

joe 100
amy 80
may

42

fred 97

(I'm assuming you want each name and optional score to be on a single
line.)

A better solution is to read an entire line at a time (use fgets() if
you can assume a maximum line length) and then apply sscanf() to the
resulting string.

An advantage of sscanf() is that it doesn't consume its input; you can
pass the same string to it again with a different format. Another
advantage is that, if the string contains a single line of input, it
won't try to read additional lines; it treats the end of the string
like end-of-file.

--
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 27 '06 #5

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

Similar topics

7
5390
by: Thomas Sourmail | last post by:
Hi, I hope I am missing something simple, but.. here is my problem: I need my program to check the last column of a file, as in : a b c d target ref 0 0 0 0 1 a 1 0 0 0 1.5 b 2 0 0 0 2 c
7
2802
by: Kay | last post by:
1) If i want to read data from a txt file, eg John; 23; a Mary; 16; i How can I read the above data stopping reading b4 each semi-colon and save it in three different variables ? 2) If I...
1
2195
by: siliconwafer | last post by:
Hi All, here is one code: int main() { FILE*fp; unsigned long a; fp = fopen("my_file.txt","w+"); a = 24; fprintf(fp,"%ld",a); while(fscanf(fp,"%ld",&a) == 1) {
37
4915
by: PeterOut | last post by:
I am using MS Visual C++ 6.0 on Windows XP 5.1 (SP2). I am not sure if this is a C, C++ or MS issue but fscanf has been randomly hanging on me. I make the call hundreds, if not thousands, of...
10
3639
by: rsk | last post by:
Hi Friends, I have written a code which suppose to read all the numbers from a hex file,But to my surprise the code is skiping every alternate value.Don't know why? Can you please help me in...
10
4429
by: Roman Zeilinger | last post by:
Hi I have a beginner question concerning fscanf. First I had a text file which just contained some hex numbers: 0C100012 0C100012 ....
59
5534
by: David Mathog | last post by:
Apologies if this is in the FAQ. I looked, but didn't find it. In a particular program the input read from a file is supposed to be: + 100 200 name1 - 101 201 name2 It is parsed by reading...
5
2301
by: a | last post by:
After reading FAQ comp.lang.c section 12 and googling again, still there is no threads talking about reading a series of numbers. The input files, somehow structured, is exemplified below: ...
3
1975
by: godblessingme | last post by:
Hi, I'm getting some problems when using fscanf to read a file. This is a piece of the program code: #include <stdlib.h> #include <stdio.h> int main() {
0
7203
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
7334
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
6993
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
5579
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3168
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
3156
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1514
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 ...
1
737
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
383
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.