fscanf

B

Bill Cunningham

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.

Bill
 
V

vippstar

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.

You're a liar, Bill. That code does not compile, so it's impossible
for you to get anything out of executing this program.
 
A

Anand Hariharan

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.

<snipped code>

Please post the *actual* code that you used to compile and generate the
executable that caused garbage to be printed. The code you posted will
not compile.

- Anand
 
B

Bill Cunningham

Please post the *actual* code that you used to compile and generate the
executable that caused garbage to be printed. The code you posted will
not compile.

Ok I did write this on the fly. I will look again at the actual code.

Bill
 
B

Bill Cunningham

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.

Bill
 
I

Ian Collins

Bill said:
#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.
So what does your documentation well you about append mode?
 
B

Bill Cunningham

So what does your documentation well you about append mode?
It just says append is a possible mode with a+ also which is append-read
mode. Plus this book tells me that garbage about using t if I want for text
mode. I know that's not portable stdc.
http://www.cppreference.com/stdio/fopen.html

Is my main online reference. I've been thinking my trouble is in fscanf. It
might be in fopen's mode.

Bill
 
I

Ian Collins

Bill said:
It just says append is a possible mode with a+ also which is append-read
mode. Plus this book tells me that garbage about using t if I want for text
mode. I know that's not portable stdc.
http://www.cppreference.com/stdio/fopen.html

Is my main online reference.

Well it's a piss-poor one if that page is anything to go by. I suggest
you look up the definition of "append"
 
P

Paul

Bill said:
It just says append is a possible mode with a+ also which is append-read
mode. Plus this book tells me that garbage about using t if I want for text
mode. I know that's not portable stdc.
http://www.cppreference.com/stdio/fopen.html

Is my main online reference. I've been thinking my trouble is in fscanf. It
might be in fopen's mode.

Bill

http://linux.die.net/man/3/fopen

"The argument mode points to a string beginning with one of the following
sequences (Additional characters may follow these sequences.):

a Open for appending (writing at end of file). The file is created if
it does not exist. The stream is positioned at the end of the file. <---- yikes

"

http://linux.die.net/man/3/fscanf

"Return Value

These functions return the number of input items successfully matched and assigned,
which can be fewer than provided for, or even zero in the event of an early matching
failure.

The value EOF is returned if the end of input is reached before either the first
successful conversion or a matching failure occurs. EOF is also returned if a read
error occurs, in which case the error indicator for the stream (see ferror(3)) is
set, and errno is set indicate the error."

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
 
B

Bill Cunningham

[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
 
B

Barry Schwarz

#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?
 
P

Paul

Bill said:
[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
 
K

Keith Thompson

Bill Cunningham said:
#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?
 
K

Keith Thompson

Bill Cunningham said:
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++.
 
V

vippstar

[...]
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 :)
 
N

Nick Keighley

    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)
 
B

Bill Cunningham

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
 
B

Bill Cunningham

[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
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top