Click to See Complete Forum and Search --> : How to read a matrix from txt file using .net streams?


zekaleon
April 4th, 2008, 08:48 AM
I've searched the internet for some usefull guide, but I haven't found anything.
In short, I have to read a matrix of doubles from a text file. But I have to use .NET API or at least I have to use OpenFileDialog.
My native code that works just fine for a console version of a program I make:

int i, j, m, n, p, q;
double *picture = 0;
double *kernel = 0;
...

ifstream inFilePicture, inFileKernel;
inFilePicture.open(argv[1], ios::in);
inFileKernel.open(argv[2], ios::in);

//Picture matrix dimension
inFilePicture>>m;
inFilePicture>>n;

//Kernel matrix dimension
inFileKernel>>p;
inFileKernel>>q;

picture = new double[m*n];
kernel = new double[p*q];

//reading matrices from .txt files
for(i=0; i<m; i++)
for(j=0; j<n; j++)
inFilePicture>>picture[i*n+j];
for(i=0; i<p; i++)
for(j=0; j<q; j++)
inFileKernel>>kernel[i*q+j];

I just don't know how to do this with .NET streams. I don't know how to parse a txt file for doubles arranged like this:
dimx
dimy
a11 ...a1,dimy
.
.
.
adimx,1...adimx, dimy

Or without the first two lines (dimx and dimy).

darwen
April 5th, 2008, 07:47 AM
Have a look at the Syste::IO::StreamReader class. It has methods like readDouble() etc which are the equivalent of the >> overloads on the istream class in C++.

Darwen.

zekaleon
April 5th, 2008, 10:27 AM
No, actually it doesn't have any such method.
We're talking here about visual studio .net 2003, and I have to use that version of visual studio. But I doubt it is an issue.