Solved

reading float number from datafile

Posted on 1998-08-17
5
285 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

733 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