473,789 Members | 2,740 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 #1
42 3819
On Aug 18, 2:39 am, "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,"%.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.
You're a liar, Bill. That code does not compile, so it's impossible
for you to get anything out of executing this program.
Aug 17 '08 #2
On Sun, 17 Aug 2008 23:39:25 +0000, "Bill Cunningham" <no****@nspam.c om>
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.
<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

Aug 17 '08 #3

"Anand Hariharan" <zn************ ********@tznvy. pbzwrote in message
news:g8******** **@aioe.org...
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
Aug 18 '08 #4

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

Bill
Aug 18 '08 #5
Bill Cunningham wrote:
"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.
So what does your documentation well you about append mode?

--
Ian Collins.
Aug 18 '08 #6

"Ian Collins" <ia******@hotma il.comwrote in message
news:6g******** ****@mid.indivi dual.net...
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

Aug 18 '08 #7
Bill Cunningham wrote:
"Ian Collins" <ia******@hotma il.comwrote in message
news:6g******** ****@mid.indivi dual.net...
>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.
Well it's a piss-poor one if that page is anything to go by. I suggest
you look up the definition of "append"

--
Ian Collins.
Aug 18 '08 #8
Bill Cunningham wrote:
"Ian Collins" <ia******@hotma il.comwrote in message
news:6g******** ****@mid.indivi dual.net...
>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
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
Aug 18 '08 #9

"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
Aug 18 '08 #10

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
3061
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
5459
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
3229
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
5594
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
9666
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
9511
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
10200
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
10139
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
6769
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
5418
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
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3701
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2909
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.