G
Guest
Hi!
Help is kindly requested in reading floating point numbers from a text file.
File is organized like this :
2000 // number of data
-1.00000 -2.000000 -0.008944 // x, y & z-coordinates to the end of file
...
...
...
...
I am also including the code snippet in case it may help to find out what is
wrong :
Help is kindly requested in reading floating point numbers from a text file.
File is organized like this :
2000 // number of data
-1.00000 -2.000000 -0.008944 // x, y & z-coordinates to the end of file
...
...
...
...
I am also including the code snippet in case it may help to find out what is
wrong :
Code:
FileStream fsr = new FileStream("test.txt", FileMode.Open, FileAccess.Read,
FileShare.Read);
StreamReader sr = new StreamReader(fsr);
String input = sr.ReadLine();
nCoor = Convert.ToInt32(input);
coordinates = new float[nNodes, 3];
for (int i = 0; i < nCoor ; i++)
{
string data = sr.ReadLine();
string[] fields = data.Split(' '); // Space is the seperator
for (int j = 0; j < 3; j++)
{
float cor = (float)Double.Parse(fields[j],
System.Globalization.NumberStyles.Float);
coordinates[i,j] = cor;
}
[\CODE]
Since this is just a test no error checking is implemented, yet.
The problem here is that given three number / line, the fields has a length
of 5!!
The system throws an exception after the first number of the first line as
follows (directly from the debug mode message :)
[DEBUGGER MESSAGE]
An unhandled exception of type 'System.FormatException' occurred in
mscorlib.dll
Additional information: Input string was not in a correct format.
[\DEBUGGER MESSAGE]
The file is also generated by StreamWriter code so the file is error free!
My request is this : What is the best way of reading floating point numbers
from a text files? Numbers have signs [+-] and can be very precise in the
case of doubles.
Thanks for any help!