Alexandre Mselmi
Alexandre Mselmi

Reputation: 15

Get numbers from .dat file with C#

I want to read Coordinate Values from .dat file. The problem is I can't split characters to identify coordinates.

example http://img4.hostingpics.net/pics/3733791211.png

Upvotes: 1

Views: 732

Answers (2)

Alexandre Mselmi
Alexandre Mselmi

Reputation: 15

First version

in the first version i just use .Replace(".", ",") method to replace the dot by the comma

public Double[] GridValues(int fromline) 
{
    Double[] values = new Double[7];
    for (int i = 1; i < 7; i++)
    {
        string input = ReadLine(fromline).Substring(8 * i, 8).Replace(".", ",");
        values[i-1] = double.Parse(input);
    }

    return values;
}

Second version

In the seconde vesion i pass an IFormatProvider to the Parse() method that defines . as the

decimal separator

public Double[] GridValues(int fromline) 
{
    Double[] values = new Double[7];
    for (int i = 1; i < 7; i++)
    {
        string input = ReadLine(fromline).Substring(8 * i, 8);
        values[i-1] = double.Parse(input,CultureInfo.InvariantCulture);
    }

    return values;
}

Upvotes: 0

Chris Wenham
Chris Wenham

Reputation: 24017

Your image appears to be of a fixed-width file, so once you know the offsets of each column you can extract them with String.Substring(offset,length).

Upvotes: 2

Related Questions