fscanf

B

Barry Schwarz

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.

Does the shoe fit or what?

http://www.nytimes.com/2008/08/03/m...l?scp=2&sq=sunday magazine aug 3, 2008&st=cse

"In the late 1980s, Internet users adopted the word "troll" to denote
someone who intentionally disrupts online communities.
Early trolling was relatively innocuous, taking place inside of small,
single-topic Usenet groups. The trolls employed what the M.I.T.
professor Judith Donath calls a "pseudo-naïve" tactic, asking stupid
questions and seeing who would rise to the bait. The game was to find
out who would see through this stereotypical newbie behavior, and who
would fall for it. As one guide to trolldom puts it, "If you don't
fall for the joke, you get to be in on it."

Today the Internet is much more than esoteric discussion forums. It is
a mass medium for defining who we are to ourselves and to others.
Teenagers groom their MySpace profiles as intensely as their hair;
escapists clock 50-hour weeks in virtual worlds, accumulating gold for
their online avatars. Anyone seeking work or love can expect to be
Googled. As our emotional investment in the Internet has grown, the
stakes for trolling - for provoking strangers online - have risen.
Trolling has evolved from ironic solo skit to vicious group hunt."
 
B

Bill Cunningham

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


Here's the code re-written a little but the results are the same.

#include <stdio.h>
#include <stdlib.h>

int main()
{
FILE *fp;
double x, y;
int rv;
char *string = "%.2f\t%.2f";
if ((fp = fopen("zo", "r")) == NULL) {
puts("open error");
exit(-1);
}
rv = fscanf(fp, string, &x, &y);
fclose(fp);
printf("%i\n", rv);
printf(string, x, y);
}

The the rv that catches the return value of fscanf reported 0. The same
garbage was printed from the x and y variables.

0.00 4.87

Bill
 
B

Bill Cunningham

Does the shoe fit or what?

http://www.nytimes.com/2008/08/03/m...l?scp=2&sq=sunday magazine aug 3, 2008&st=cse

"In the late 1980s, Internet users adopted the word "troll" to denote
someone who intentionally disrupts online communities.
Early trolling was relatively innocuous, taking place inside of small,
single-topic Usenet groups. The trolls employed what the M.I.T.
professor Judith Donath calls a "pseudo-naïve" tactic, asking stupid
questions and seeing who would rise to the bait. The game was to find
out who would see through this stereotypical newbie behavior, and who
would fall for it. As one guide to trolldom puts it, "If you don't
fall for the joke, you get to be in on it."

Today the Internet is much more than esoteric discussion forums. It is
a mass medium for defining who we are to ourselves and to others.
Teenagers groom their MySpace profiles as intensely as their hair;
escapists clock 50-hour weeks in virtual worlds, accumulating gold for
their online avatars. Anyone seeking work or love can expect to be
Googled. As our emotional investment in the Internet has grown, the
stakes for trolling - for provoking strangers online - have risen.
Trolling has evolved from ironic solo skit to vicious group hunt."
??

Who are you addressing Barry?

Bill
 
B

Bill Cunningham

Keith Thompson said:
Whoops, thanks for the correction.

Keith I hope this posts with no header problem above. I have k&r2. I use
the www.cppreference.com C section to look up return values mainly. I find
linux's man pages very vague IMO on my linux anyway, that is old.

Bill
 
B

Ben Bacarisse

Bill Cunningham said:
Here's the code re-written a little but the results are the same.
<snip>

Compare with:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
FILE *fp;
double x, y;
int rv;
if ((fp = fopen("zo", "r")) == NULL) {
perror("open error");
return EXIT_FAILURE;
}
rv = fscanf(fp, "%lf\t%lf", &x, &y);
fclose(fp);
printf("%i\n", rv);
printf("%.2f\t%.2f\n", x, y);
return EXIT_SUCCESS;
}

fscanf formats are not the same as printf formats.
 
B

Bill Cunningham

Bill Cunningham said:
??

Who are you addressing Barry?

Bill

If you are making a comment to simply say something I'm not listening.
If you want to help and are saying something critical I'm listening.

There are 2 main people on usenet. The one who asks for help and those
who want to help. Then there's noise. Meaningless, helpless, nonsense.

Bill
 
B

Bill Cunningham

fscanf formats are not the same as printf formats.
I hadn't noticed that Ben for some reason. So what I should do is use
%lf or the float type for x and y then.

Now I feel silly for asking a question like this but I was really stuck.
But isn't %f still a float conversion in *scanf ? The program I wrote to
print the file "zo" and the text it contains uses fprintf %f conversion for
2 doubles. Here's a snippet...

double x,y;
x=strtod(argv[1],NULL);
y=strtod(argv[2],NULL);
fprintf(argv[3],"%.2f\t%.2f",x,y);

Float conversion can be used on doubles.
But I guess not in fscanf.

Bill
 
P

Paul

Bill said:
Here's the code re-written a little but the results are the same.

#include <stdio.h>
#include <stdlib.h>

int main()
{
FILE *fp;
double x, y;
int rv;
char *string = "%.2f\t%.2f";
if ((fp = fopen("zo", "r")) == NULL) {
puts("open error");
exit(-1);
}
rv = fscanf(fp, string, &x, &y);
fclose(fp);
printf("%i\n", rv);
printf(string, x, y);
}

The the rv that catches the return value of fscanf reported 0. The same
garbage was printed from the x and y variables.

0.00 4.87

Bill

So now you know that zero parameters were converted by fscanf.
X and Y cannot have good values, because no conversion was done.

This guy wrote a similar example, but even he did not consider that
fscanf might convert zero items. He is checking for EOF with
feof(file). If you had continued to use the "append" mode for
your input file, then inserting an feof(file) check might
have indicated a problem (i.e. already at the end of the file).

(second example down...)
http://irc.essex.ac.uk/www.iota-six.co.uk/c/i4_fwrite_fscanf_fprintf.asp

Since you changed to "r" mode for opening the file, then the
file pointer is probably not at the end of the file any more.

And that leaves the "string" specification as being the dodgy bit.
Try some other specifications first. Try converting just one
number with "%f" and work from there. This stuff looks tricky.

See PDF page 299, for a few examples of fscanf usage. Or search for
the string "25 54.32E-1 thompson" which is input to the first example.

http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf

I was amazed at all the stuff I could find.

Paul
 
B

Ben Bacarisse

Bill Cunningham said:
I hadn't noticed that Ben for some reason. So what I should do is use
%lf or the float type for x and y then.

No. You really do need a reference manual. I used lf because you
don't have floats. For scanf, %lf expects a pointer to a double (as
you have).
Now I feel silly for asking a question like this but I was really stuck.
But isn't %f still a float conversion in *scanf ?

Yes it is. It would work if you had float variables but you don't,
you have doubles.
The program I wrote to
print the file "zo" and the text it contains uses fprintf %f conversion for
2 doubles. Here's a snippet...

double x,y;
x=strtod(argv[1],NULL);
y=strtod(argv[2],NULL);
fprintf(argv[3],"%.2f\t%.2f",x,y);

Float conversion can be used on doubles.

By "float conversion" do you mean %f? If so, then it *requires* a
double in the printf. The trick is that floats get converted to
double in a call to printf anyway so there is little danger of getting
it wrong.
 
K

Keith Thompson

Bill Cunningham said:
No Keith I've already said it contains,

.26 0.00

So you did.

However, you only mentioned that in your original article, in which
you posted non-compilable code. I didn't take the time to go back and
re-read it.

[snip]
 
K

Keith Thompson

Bill Cunningham said:
Keith I hope this posts with no header problem above. I have
k&r2. I use the www.cppreference.com C section to look up return
values mainly. I find linux's man pages very vague IMO on my linux
anyway, that is old.

As I recall, K&R2's reference section does a perfectly good job of
describing return types for standard library functions.

I reiterate my advice: Stop using any reference materials other than
K&R2. Well, not quite; the comp.lang.c FAQ is also an excellent and
reliable resource. If those two resources together are not
sufficiently helpful, then ask here -- but *only* after you've
carefully checked the types and meanings of any arguments and return
types for all standard functions you're calling.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

URGENT 1
no error by fscanf on reading from output file 18
[C language] Issue in the Lotka-Volterra model. 0
code 34
code question 74
beginner fscanf question 10
error 28
code snippet 92

Members online

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,835
Latest member
lila30

Latest Threads

Top