Trying to read a .txt file that contains problem data varying in size and type (i.e. single integer, double/integer/binary arrays) for a computational experiment. By reviewing examples available here and here, I developed two alternative code blocks, but for some reason (I guess the Trim method does not work) they don't work. It gives the error of 'Input string was not in a correct format.' Does anyone let me know what I did wrong or what I missed?
using System;
using System.IO;
namespace Program
{
class Program
{
// Basic parameters
private static int x, y, z;
private static double[,] array= new double[x, x];
static void Main(string[] args)
{
string fileName = "LUU1_5.txt";
ReadFile(fileName);
}
static void ReadFile(string sourcefile)
{
using (TextReader reader = File.OpenText(sourcefile))
{
// Method-1 Read line by line
//x= int.Parse(reader.ReadLine());
//y= int.Parse(reader.ReadLine());
//z= int.Parse(reader.ReadLine());
//int k=0;
//for (int row2 = 3; row2 < 3 + 1 + x; row2++)
//{
// k = 0;
// foreach (var row2 in reader.ReadLine())
// {
// foreach (var col in row2)
// {
// Console.WriteLine(col.Trim());
// //array[row2 - 3, k] = double.Parse(col.Trim()); //
// k++;
// }
// }
//}
// Method 2-Read All Lines
String[] content = File.ReadAllLines(sourcefile);
x= int.Parse(content[0]);
y= int.Parse(content[1]);
z= int.Parse(content[2]);
int j = 0;
for (int row = 3; row < 3 + 1 + x; row++)
{
j = 0;
foreach (var col in content[row].Trim().Split(" "))
{
Console.WriteLine(content[row]);
Console.WriteLine(col);
Console.WriteLine(col.Trim()); // using console for monitoring the output
Console.WriteLine(content[row].Trim().Split(" "));
array[row - 3, j] = double.Parse(col.Trim());
//array[row-3, j] = double.TryParse(col.Trim(), out array[row-3, j]);
j++;
}
}
// Then the program reads the next 0-1 array starting line 10.
reader.Close();
}
}
}
}
The data file looks like this:
5
2
100000
0 46.1 33.53 48.37 44.82 47.8
46.1 0 15.65 21.38 42.05 85.48
33.53 15.65 0 17.2 45.18 77.62
48.37 21.38 17.2 0 60.64 94.26
44.82 42.05 45.18 60.64 0 57.08
47.8 85.48 77.62 94.26 57.08 0
0 1 0 0 0 0
0 0 1 1 1 1
0 0 0 1 1 1
0 0 0 0 1 1
0 0 0 0 0 1
0 0 0 0 0 0
Also, I will be reading larger data files such as 500x500 or 1000x1000 for array-x later in this experiment, do you think I have any speed or memory issues? Which method should I use (line by line or all lines in one)? I'd like to design the read block accordingly now.
Thanks in advance!
I think it's better to use DTO and regex.