Solved

reading float number from datafile

Posted on 1998-08-17
5
283 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
ID: 1170614
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
ID: 1170615
Sorry newexpert, but I tried it and it didn't work!
0
 
LVL 23

Accepted Solution

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

Replace

double x,y;

with

float x,y;

0
 

Author Comment

by:sunjian061197
ID: 1170617
Thank you chensu! You really solved my problem.

sunjian
0
 
LVL 23

Expert Comment

by:chensu
ID: 1170618
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Building cUrl in Windows v7.43.0 4 31
C++ error cannot convert from std::string to const char * 6 183
Exception thrown at 0x00007FFD5BC81F28 7 44
Problem with SqlConnection 4 168
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

776 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