473,657 Members | 2,420 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 4225
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******** *************@i 3g2000cwc.googl egroups.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*******@gmai l.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_Keit h) 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
5429
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
2818
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 enter a number, can I use to call a particular node ? eg enter a number: 3 calling node of number 3 is it possible ?
1
2202
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
4953
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 times but it hangs in different places with the same data. The offending code follows. ReadFile(char *csFileName) { float fFloat1, fFloat2;
10
3658
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 solving this problem. The code is as follows;
10
4443
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
5570
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 the + character, and then sending the remainder into fscanf() like
5
2308
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: <presence/absence of n space/tab on the first n lines> 12 <presence/absence of n space/tab here>0<presence/absence of n space/tab here>90 10 23 43 0 0 0 0 0 0 0 90 0 0 0 0 88 0 0 0 ...
3
1985
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
8826
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8732
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7330
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5632
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4155
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4306
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
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 we have to send another system
2
1615
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.