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
Solved

reading float number from datafile

Posted on 1998-08-17
5
284 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

840 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