Solved

reading float number from datafile

Posted on 1998-08-17
5
275 Views
Last Modified: 2010-04-10
Hi,
  This should be rather easy but it is just driving me mad.
  I just want to read float numbers from a data file. My data file looks like this:

 -1.3635760e-002  8.9981614e-001
  3.7464906e-001  8.7696881e-002
  8.8600742e-001  2.4223525e-001
  5.4332379e-001  8.5243329e-001
  6.8148103e-001  6.7536886e-001
  8.3899312e-001  4.7851886e-001
  8.2437103e-001  9.4562145e-002
  2.5349397e-001  2.8462880e-001

Now I want to read all the numbers out and print them. My code looks like this:

FILE* inputFile;
double x,y;

inputFile=fopen("e:\\tmp\\test.dat","r");
if(inputFile == NULL) {
  printf("Error opening input data file. Exiting...\n");
  exit(1);
}
while(!feof(inputFile)) {
  fscanf(inputFile,"%e",&x);
  fscanf(inputFile,"%e",&y);
  printf("x=%f. y=%f.\n",x,y);
}
fclose(inputFile);

However, the outputs are like this:
x=-0.000000. y=-1.474790.
x=-0.000000. y=-1.474790.
x=-0.000000. y=-1.474790.
x=-0.000000. y=-1.474790.
x=-0.000000. y=-1.474790.
x=-0.000000. y=-1.474790.
...

Can anybody tell me where is my mistake? And what is the proper(hopefully best) way of doing this job? I am using MSVC5.0.

Thanks in advance.

sunjian
0
Comment
Question by:sunjian061197
  • 2
  • 2
5 Comments
 
LVL 1

Expert Comment

by:newexpert
Comment Utility
Try this:

while(!feof(inputFile)) {
  fscanf(inputFile,"%e %e ",&x, &y);
  printf("x=%f. y=%f.\n",x,y);
}

ie put spaces after and between %e.  This will tell fscanf to use white spaces (space, tab, newline character) as delimiters.

0
 

Author Comment

by:sunjian061197
Comment Utility
Sorry newexpert, but I tried it and it didn't work!
0
 
LVL 23

Accepted Solution

by:
chensu earned 50 total points
Comment Utility
"%e" expects float instead of double.

Replace

double x,y;

with

float x,y;

0
 

Author Comment

by:sunjian061197
Comment Utility
Thank you chensu! You really solved my problem.

sunjian
0
 
LVL 23

Expert Comment

by:chensu
Comment Utility
When used with scanf, "%e" expects float. However, when used with printf, "%e" expects double. This is annoying.

And this is better.
  printf("x=%f. y=%f.\n",(double)x,(double)y);

0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now