473,803 Members | 4,591 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

fscanf

I'm doing something wrong and all I know to do is turn to clc. I have a
text file containing 2 doubles separated by a tab.

..26 0

Is the text. I want to read the two double and printf them out. Here's my
file.

#include <stdio.h>

int main() {
FILE *fp;
double x,y;
fp=fopen("zo"," r"); /*error checking out for brevity */
fscanf(fp,"%.2f \t%.2f",&string );
fclose(fp);
printf("%.2f%.2 f",x,y);
}

All I get is garbage that is contained in x and y. For whatever simple
reason that is beyond me evidently I can't read and printf out to stdin from
this text file. I don't think fread is really necessary.

Bill
Aug 17 '08
42 3821
On Mon, 18 Aug 2008 00:49:03 GMT, "Bill Cunningham" <no****@nspam.c om>
wrote:
>
"Bill Cunningham" <no****@nspam.c omwrote in message
news:yx3qk.131 $w51.17@trnddc0 1...
>Ok I did write this on the fly. I will look again at the actual code.
#include <stdio.h>

int main() {
FILE *fp;
double x,y;
fp=fopen("zo"," a");
Are you reading from or writing to fp?
fscanf(fp,"%.2f \t%.2f",&x,&y);
Do you have a reference that describes fscanf? Did you read it? Does
fscanf allow "." inside a conversion specification? Is "f" the
correct conversion specifier for a double?
fclose(fp);
printf("%.2f\t% .2f",x,y);
}

Now this compiled for me with the results 0.00 and 4.87. Not the text
What does the input file look like?
>from the file called "zo". The only real difference here is the text mode is
Difference from what?
>append and not read.
And you chose this mode why?

--
Remove del for email
Aug 18 '08 #11
Bill Cunningham wrote:
"Paul" <no****@needed. comwrote in message news:g8******** **@aioe.org...

[snip]
>Defensive programming, means checking the values returned by things like
fscanf.
Wouldn't you be curious, whether fscanf converted zero, one, or two items
? If the
answer is not two, then X or Y could contain bogus information. And if an
end of
file was encountered, you'd probably want to know about that also. There
are many
possible outcomes, when handling file I/O.

Paul

Like this you mean?

fscanf(fp,"%.2f \t%.2f",&x,&y);
if (fp==EOF)
puts("fscanf error");
Bill

fscanf also returns an integer value. You haven't
taken advantage of the integer it returns.

returned_value = fscanf(fp,"%.2f \t%.2f",&x,&y)

The manual page says the returned_value can tell you some things
about how things went, when the fscanf ran. You could check
returned_value, to see how many arguments it got.

This is not defensive programming, but you could
do something like use "printf" to print the value
of the integer "returned_value ", and see whether it
is the value you expected. If the value printed was 2,
then you'd know you got two conversions, so both "x"
and "y" got loaded with goodies.

Try defining an integer called return_value, and
see what is coming back from fscanf. Use printf
to print out the value of "returned_value ".

Once you've figured out what went wrong, you can
add conditional statements to your program, to
protect it against invalid input or unexpected
results like EOF.

In your next posting, you can tell us what printf printed,
and your interpretation of what it means.

Paul
Aug 18 '08 #12
Paul wrote:

<sound advice>

Don't spoon feed the troll :)

--
Ian Collins.
Aug 18 '08 #13
"Bill Cunningham" <no****@nspam.c omwrites:
"Bill Cunningham" <no****@nspam.c omwrote in message
news:yx3qk.131$ w51.17@trnddc01 ...
>Ok I did write this on the fly. I will look again at the actual code.
#include <stdio.h>

int main() {
FILE *fp;
double x,y;
fp=fopen("zo"," a");
fscanf(fp,"%.2f \t%.2f",&x,&y);
fclose(fp);
printf("%.2f\t% .2f",x,y);
}

Now this compiled for me with the results 0.00 and 4.87. Not the text
from the file called "zo". The only real difference here is the text mode is
append and not read.
No, there at least two real differences. One is that you're not
referring to an undeclared variable called "string". The other is
that you change the mode for fopen from "r" (which would have been
correct) to "a" (which makes no sense).

"a" is append mode; it means you want to write new data to the end of
an existing file. Why would you use append mode when you want to
*read* from the file?

BTW, do you expect us to *guess* what's in your "zo" file?

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 18 '08 #14
"Bill Cunningham" <no****@nspam.c omwrites:
[...]
Like this you mean?

fscanf(fp,"%.2f \t%.2f",&x,&y);
if (fp==EOF)
puts("fscanf error");
No.

What type is fp? What type is EOF? What makes you think that
comparing fp to EOF is meaningful?

Stop guessing. Get a decent reference and READ IT.

If you have a copy of K&R2, my advice is to use it as your one and
only reference. Don't waste your time with cvppreference.c om; it's
for C++.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 18 '08 #15
On Aug 18, 5:23 am, Keith Thompson <ks...@mib.orgw rote:
"Bill Cunningham" <nos...@nspam.c omwrites:

[...]
Like this you mean?
fscanf(fp,"%.2f \t%.2f",&x,&y);
if (fp==EOF)
puts("fscanf error");

No.

What type is fp? What type is EOF? What makes you think that
comparing fp to EOF is meaningful?

Stop guessing. Get a decent reference and READ IT.

If you have a copy of K&R2, my advice is to use it as your one and
only reference. Don't waste your time with cvppreference.c om; it's
for C++.
correction (typo): cppreference.co m not cVppreference.c om.
Also, I'm not sure what cppreference.co m is for, but it does not seem
to be an appropriate resource for C++, so please don't suggest it.
Just... don't waste time with cppreference :-)
Aug 18 '08 #16
vi******@gmail. com writes:
On Aug 18, 5:23 am, Keith Thompson <ks...@mib.orgw rote:
[...]
>Stop guessing. Get a decent reference and READ IT.

If you have a copy of K&R2, my advice is to use it as your one and
only reference. Don't waste your time with cvppreference.c om; it's
for C++.

correction (typo): cppreference.co m not cVppreference.c om.
Whoops, thanks for the correction.

[...]

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 18 '08 #17
On 18 Aug, 00:39, "Bill Cunningham" <nos...@nspam.c omwrote:
* * I'm doing something wrong and all I know to do is turn to clc. I have a
text file containing 2 doubles separated by a tab.

.26 * *0

Is the text. I want to read the two double and printf them out. Here's my
file.

#include <stdio.h>

int main() {
* * * FILE *fp;
* * * double x,y;
* * * *fp=fopen("zo", "r"); /*error checking out for brevity */
* * * *fscanf(fp,"%.2 f\t%.2f",&strin g);
* * * *fclose(fp);
* * * *printf("%.2f%. 2f",x,y);

}

All I get is garbage that is contained in x and y. For whatever simple
reason that is beyond me evidently I can't read and printf out to stdin from
this text file. I don't think fread is really necessary.
0. fix your layout
1. post your code
2. post your input data
3. post your output
4. explain why you don't like 3
5. check return values
6. RTFM
7. don't guess (see 6)

--
Nick Keighley
Aug 18 '08 #18

"Keith Thompson" <ks***@mib.orgw rote in message
news:ln******** ****@nuthaus.mi b.org...
BTW, do you expect us to *guess* what's in your "zo" file?
No Keith I've already said it contains,

..26 0.00

In using the code I posted before I got...

0.00 4.87

If I would've set xand y to 0 that is what would've been printed with
printf. I see the fopen mode I should've used was "r". I'll keep working
with it.

Bill
Aug 18 '08 #19

"Paul" <no****@needed. comwrote in message news:g8******** **@aioe.org...

[snip]
>
fscanf also returns an integer value. You haven't
taken advantage of the integer it returns.

returned_value = fscanf(fp,"%.2f \t%.2f",&x,&y)

The manual page says the returned_value can tell you some things
about how things went, when the fscanf ran. You could check
returned_value, to see how many arguments it got.

This is not defensive programming, but you could
do something like use "printf" to print the value
of the integer "returned_value ", and see whether it
is the value you expected. If the value printed was 2,
then you'd know you got two conversions, so both "x"
and "y" got loaded with goodies.

Try defining an integer called return_value, and
see what is coming back from fscanf. Use printf
to print out the value of "returned_value ".

Once you've figured out what went wrong, you can
add conditional statements to your program, to
protect it against invalid input or unexpected
results like EOF.

In your next posting, you can tell us what printf printed,
and your interpretation of what it means.

Paul
Ok I'll try it. I'm not used to using the *scanf family. I usually use fgets
or fgetc.

Bill
Aug 18 '08 #20

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

Similar topics

3
6736
by: Benedicte | last post by:
Hi, I'm getting some problems when using fscanf to read a file. This is a piece of the program code: main () { /*** Variable declaration ***/ FILE *vpfile; /*** Data file ***/
4
3062
by: Psibur | last post by:
Hello, trying to get back into c and was having issue with reading a simple text file with an aribtrary # of lines with 3 int's per line, with the eventual purpose of putting each int into an element of an array (eventually will be other things, but I'm sticking to int's for now). I.e.: 0 1 1 1 1 1 2 1 1 etc... The problem is it'll read and print all but the last line. Is there
7
5462
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
1
2215
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) {
9
3231
by: quyvle | last post by:
I can't seem to get this function to work correctly. I'm wondering if anyone could help me out with this. So I'm using the fscanf function to read the input stream and store each string in the appropriate variables. Here's what I'm reading from another file: "# Number of power catergories: 9"
9
2372
by: kvnsmnsn | last post by:
Over the course of my career I've transitioned from an Ada programmer (am I dating myself?) to a C programmer to a Java programmer and now back to a C programmer with the job I've currently started. What I'd like to do is write a piece of C code that inputs to the pro- gram a line written to a file. The Java code written below does exactly what I want; it writes the <String"ab cd" to file "Java.Txt" and then reads it back in to variable...
37
4981
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;
59
5598
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
1
1276
momotaro
by: momotaro | last post by:
I have a small problem with the last fscanf in this function...every thing is logic in there but can't find the problem... plz help node *BuildGraph() { int range, vehicules, i, j, k;
0
9564
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10316
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...
1
10295
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10069
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
9125
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...
1
7604
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6842
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
5629
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.