Reading numbers from text file

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 :

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!
 
B

Bruce Wood

Are you sure you wouldn't rather call

data.Split()

? According to the doc this will skip over leading and trailing white
space, as well as skip over multiple white space characters as if they
were one.

The problem with

data.Split(' ')

is that if there are two adjacent spaces, it will return an empty
string representing the "data" between the two spaces.
 
M

Morten Wennevik

It looks to me like you are trying to convert the blank line between the number of entries and the actual data. Since it is just a blank line you will get an error when you try to convert it to a number.

Instead of using ReadLine I would probably just ReadToEnd and split it along Environment.NewLine

string[] lines = sr.ReadToEnd().Split(Environment.NewLine.ToCharArray());
int count = Int32.Parse(lines[0]);
for(int i = 2; i < lines.Length; i++)
{
string[] numbers = lines.Split(' ');
etc...
}

Also, as Bruce said you could simply do

string[] data = sr.ReadToEnd().Split(null);
int count = data[0] = Int32.Parse(lines[0]);
for(int i = 2, x = 0; i < lines.Length; x++)
{
coordinates[x,0] = Single.Parse(data[i++]);
coordinates[x,1] = Single.Parse(data[i++]);
coordinates[x,2] = Single.Parse(data[i++]);
}

PS! code is untested
 
G

Guest

Thank you very much. Although it took sometime to understand, it appears that
at least the first part has started to work!

Regards
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top