473,803 Members | 4,458 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

"Bill Cunningham" <no****@nspam.c omwrote in message
news:zulqk.194$ p72.57@trnddc05 ...
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
Aug 19 '08 #21

"Keith Thompson" <ks***@mib.orgw rote in message
news:ln******** ****@nuthaus.mi b.org...
vi******@gmail. com writes:
>On Aug 18, 5:23 am, Keith Thompson <ks...@mib.orgw rote:
>correction (typo): cppreference.co m not cVppreference.c om.

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
Aug 19 '08 #22
"Bill Cunningham" <no****@nspam.c omwrites:
"Bill Cunningham" <no****@nspam.c omwrote in message
news:zulqk.194$ p72.57@trnddc05 ...
>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.
<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.

--
Ben.
Aug 19 '08 #23
Bill Cunningham said:
I have k&r2.
Great.
I use the www.cppreference.com C section to look up return values mainly.
I suggest you stop doing that. You have a much more authoritative reference
available to you: K&R2. Use it.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Aug 19 '08 #24

"Bill Cunningham" <no****@nspam.c omwrote in message
news:yYoqk.212$ lf2.25@trnddc07 ...
??

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
Aug 19 '08 #25

"Ben Bacarisse" <be********@bsb .me.ukwrote in message
news:87******** ****@bsb.me.uk. ..
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


Aug 19 '08 #26
Bill Cunningham wrote:
"Bill Cunningham" <no****@nspam.c omwrote in message
news:zulqk.194$ p72.57@trnddc05 ...
>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

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....nf_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/wg...docs/n1124.pdf

I was amazed at all the stuff I could find.

Paul
Aug 19 '08 #27
"Bill Cunningham" <no****@nspam.c omwrites:
"Ben Bacarisse" <be********@bsb .me.ukwrote in message
news:87******** ****@bsb.me.uk. ..
>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.
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.

--
Ben.
Aug 19 '08 #28
"Bill Cunningham" <no****@nspam.c omwrites:
"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
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]

--
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 19 '08 #29
"Bill Cunningham" <no****@nspam.c omwrites:
"Keith Thompson" <ks***@mib.orgw rote in message
news:ln******** ****@nuthaus.mi b.org...
>vi******@gmail. com writes:
>>On Aug 18, 5:23 am, Keith Thompson <ks...@mib.orgw rote:

>>correction (typo): cppreference.co m not cVppreference.c om.

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.
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.

--
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 19 '08 #30

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
9703
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
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,...
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
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
5500
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
5629
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2970
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.