GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 10-Oct-2005, 00:24
clara clara is offline
New Member
 
Join Date: Oct 2005
Posts: 9
clara is on a distinguished road
Smile

How to read scientific number from file?


Hello
I'm currently doing a set of program that handles a VRML file as input. Basically, the main programs are for parsing the input file, and another is to calculate the boundary coordinate of the whole 3D world.

I'm now having a problem of reading an input file, where the file consists of scientific number (for example: -1.26759e-006).
In my program I would read the file character by character and store it in a buffer. Afterwards, I would examine the buffer word by word.
For fields that are supposed to be numbers, my program would read it as float numbers. But I don't know how if the input is a scientific number...


Thank you for any advice..I'd really appreciate it...
  #2  
Old 10-Oct-2005, 00:44
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: How to read scientific number from file?


Look up the sprintf() function. It can handle stf notation.
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
  #3  
Old 10-Oct-2005, 01:07
clara clara is offline
New Member
 
Join Date: Oct 2005
Posts: 9
clara is on a distinguished road

Re: How to read scientific number from file?


Thank you for the reply
I've looked up the sprintf() function and yeah, it can handle the e and E type...

then I suppose I can first read the input number as e or E type and if the function returns a negative number, that means it's an error and I can read the number as usual (float , etc)....
I hope I'm correct ...

Thanks again for the advice ^.^
  #4  
Old 10-Oct-2005, 09:37
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,793
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: How to read scientific number from file?


Quote:
Originally Posted by clara
Thank you for the reply
I've looked up the sprintf() function and yeah, it can handle the e and E type...

then I suppose I can first read the input number as e or E type and if the function returns a negative number, that means it's an error and I can read the number as usual (float , etc)....
I hope I'm correct ...

Thanks again for the advice ^.^

You can use fscanf(...,"%f"...,) to read floats and fscanf(...,"%lf",...) to read doubles. Input formats can be decimal or scientific notation. I usually use fgets() followed by sscanf(), but here's more-or-less what I would do:


CPP / C++ / C Code:
#include <stdio.h>
int main()
{
  double x, y;

  FILE *infile;
  char *inname = "test.txt";

  infile = fopen(inname, "r");
  if (!infile) {
    printf("Can't open file %s for reading\n", inname);
    return 0;
  }

  if (fscanf(infile, "%lf", &x) != 1) {
    printf("Can't read x from file %s\n", inname);
    return 0;
  }

  printf("x = %f, x = %e, x = %g\n", x, x, x);

  if (fscanf(infile, "%lf", &y) != 1) {
    printf("Can't read y from file %s\n", inname);
    return 0;
  }

  printf("y = %f, y = %e, y = %g\n", y, y, y);

  return 0;
}


And here's my file "test.txt" I used your example number in both formats.

Code:
-1.26759e-006 -.00000126759

Here's my output:

Quote:
x = -0.000001, x = -1.267590e-06, x = -1.26759e-06
y = -0.000001, y = -1.267590e-06, y = -1.26759e-06

Regards,

Dave
  #5  
Old 10-Oct-2005, 10:14
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: How to read scientific number from file?


Dang, I did it again. I meant sscanf(). Sorry.

Thanks Dave...
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
  #6  
Old 10-Oct-2005, 21:50
clara clara is offline
New Member
 
Join Date: Oct 2005
Posts: 9
clara is on a distinguished road

Re: How to read scientific number from file?


thanks Dave and Walt
It really helps ^.^
Walt: yeah, I didn't realise that what I was looking at (even after I read your first post) was actually sscanf lol

By the way, I have another related question...
can the scientific number then be processed as a float? for example, if I want to convert a char array that contains "1.267590e-06", can I use atof function?
  #7  
Old 10-Oct-2005, 22:04
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: How to read scientific number from file?


Try it. Write a test program.
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
  #8  
Old 11-Oct-2005, 03:24
clara clara is offline
New Member
 
Join Date: Oct 2005
Posts: 9
clara is on a distinguished road

Re: How to read scientific number from file?


yeah hehe...I'm waiting for my program to finish running first sorry for asking that kind of question
 
 

Recent GIDBlogToyota - 2008 November Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 08:44
How to detect end of file with read() function call? nkhambal C Programming Language 6 12-Oct-2004 02:08
CD burner wont burn!! robertli55 Computer Hardware Forum 1 18-Jun-2004 11:53
Yet another CD burner problem: Lite-On LSC-24082K Erwin Computer Hardware Forum 1 22-May-2004 12:28

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 10:51.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.