![]() |
|
#1
|
|||
|
|||
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
|
||||
|
||||
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
|
|||
|
|||
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
|
|||
|
|||
Re: How to read scientific number from file?Quote:
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:
And here's my file "test.txt" I used your example number in both formats. Code:
Here's my output: Quote:
Regards, Dave |
|
#5
|
||||
|
||||
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
|
|||
|
|||
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
|
||||
|
||||
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
|
|||
|
|||
Re: How to read scientific number from file?yeah hehe...I'm waiting for my program to finish running first
|
Recent GIDBlog
Toyota - 2008 November Promotion by Nihal
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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