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/373开发者_StackOverflow社区3791211.png
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)
.
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;
}
精彩评论