Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > fscanf

Reply
Thread Tools

fscanf

 
 
Barry Schwarz
Guest
Posts: n/a
 
      08-18-2008
On Mon, 18 Aug 2008 00:49:03 GMT, "Bill Cunningham" <(E-Mail Removed)>
wrote:

>
>"Bill Cunningham" <(E-Mail Removed)> wrote 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");


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
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      08-18-2008
Paul wrote:

<sound advice>

Don't spoon feed the troll

--
Ian Collins.
 
Reply With Quote
 
 
 
 
Paul
Guest
Posts: n/a
 
      08-18-2008
Bill Cunningham wrote:
> "Paul" <(E-Mail Removed)> wrote in message news:g8ajiu$uga$(E-Mail Removed)...
>
> [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
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      08-18-2008
"Bill Cunningham" <(E-Mail Removed)> writes:
> "Bill Cunningham" <(E-Mail Removed)> wrote 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_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <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"
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      08-18-2008
"Bill Cunningham" <(E-Mail Removed)> writes:
[...]
> 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.com; it's
for C++.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <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"
 
Reply With Quote
 
vippstar@gmail.com
Guest
Posts: n/a
 
      08-18-2008
On Aug 18, 5:23 am, Keith Thompson <(E-Mail Removed)> wrote:
> "Bill Cunningham" <(E-Mail Removed)> writes:
>
> [...]
>
> > 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.com; it's
> for C++.


correction (typo): cppreference.com not cVppreference.com.
Also, I'm not sure what cppreference.com 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
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      08-18-2008
(E-Mail Removed) writes:
> On Aug 18, 5:23 am, Keith Thompson <(E-Mail Removed)> wrote:

[...]
>> 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.com; it's
>> for C++.

>
> correction (typo): cppreference.com not cVppreference.com.


Whoops, thanks for the correction.

[...]

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <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"
 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      08-18-2008
On 18 Aug, 00:39, "Bill Cunningham" <(E-Mail Removed)> wrote:
> * * 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%.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
 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      08-18-2008

"Keith Thompson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

> 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


 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      08-18-2008

"Paul" <(E-Mail Removed)> wrote in message news:g8akpm$ada$(E-Mail Removed)...

[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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
fscanf equivalent in c++ John Phung C++ 11 04-09-2004 06:54 PM
Question about fscanf .. Marc Reclaire C++ 2 12-18-2003 12:51 PM
How to use ifstream::get() like fscanf()? cylin C++ 7 09-19-2003 05:42 AM
Re: How to put comments in initialization file read by fscanf()? Chris Torek C Programming 0 07-14-2003 05:33 AM
fscanf question Fallon C Programming 3 07-13-2003 05:55 PM



Advertisments