c# - How to read from text file to array? -
what's everybody,
i have problem in code , couldn't figure out how read text file , put in 2-dimension array of double type array [1024,8] in c# .
0 148.9 19.4 20.2 112.6 41.9 205.7 46.7 87.2 1 41.4 97.1 86.4 102.5 99.1 183.1 47.7 84.0 2 154.8 303.1 252.2 110.7 74.5 59.7 193.7 361.6 . . 1023 40.8 136.8 222.1 39.5 104.9 35.3 76.0 111.4
i tried read file line line, way didn't me
static void main(string[] args) { int counter = 0; string line; double[] task = new double[8]; // read file , display line line. system.io.streamreader file = new system.io.streamreader("c:\\test.txt"); //int count = 0; while ((line = file.readline()) != null && counter <=1023 ) { //count++; //console.writeline(count); string [] numbers = new string [8]; int numcount = 0; (int = 0; < line.length; i++) { if (line[i] != ' ') { numbers[numcount]=""; while (line[i] != ' ' ) { numbers[numcount] += line[i]; i++; } numcount++; } } (int = 0; < 8; i++) { task[i] = convert.todouble(numbers[i]); } counter++; console.writeline("the array contain:"); (int = 0; < 8; i++) console.writeline(task[i]); } file.close(); // suspend screen. console.readline(); }
your code seems tidy! replace code
for (int = 0; < line.length; i++)
, place this
int i=0; while (i < line.length) { if (line[i] != ' ') { numbers[numcount] = ""; while (line[i] != ' ') { numbers[numcount] += line[i]; i++; if (i >= line.length) break; } numcount++; } i++; } (int ui = 0; ui < 8; ui++) { task[ui] = convert.todouble(numbers[ui]); } counter++; console.writeline("the array contain:"); (int ui = 0; ui < 8; ui++) console.writeline(task[ui]);
Comments
Post a Comment