New to .NET and file IO

simon77

New member
Joined
Jan 17, 2005
Messages
3
OK, Im trying to read a file in, three columns of numbers in all, each column into a seperate array.

Last week I would have done something like:
char filename[25];
std::ifstream afile(filename); //
and then just loop till the end of the file using the >> operator.

But now weve got .NET installed and having my first a attempt at forms and dialouge boxes rather than my usual console stuff. Anyway Im using streamreader to read the file in but cant grab less than a line at a time.

StreamReader* objReader = new StreamReader
String *sLine = "";
sLine = objReader->ReadLine();

Surely I dont have to search through the string for a empty space and then split the string into smaller ones and then convert to a double. There must be a better way. Please enlighten me. Cheers

Apologies for being dim. I promise Ive googled long and hard for the answer...
 
It may help if you give a little more detail about how the file is structured and what kind of variables etc. you are attempting to read into.
If you wish to read less than a line the StreamReader has a .Read method that gives you a bit more control over the reading process, or as an alternative you could do a .ReadToEnd and then use something like regular expressions to parse the resulting string.
 
basically, its a series of x y z coordinates that I want in three arrays: double x[], double y[] and double z[]. So only three numbers to a line but could be any size numbers and any number of lines. Ill go check out the .Read method. Thanks.
 
simon77 said:
Surely I dont have to search through the string for a empty space and then split the string into smaller ones and then convert to a double. There must be a better way. Please enlighten me. Cheers

That is exactly what you need to do but vb.net makes it easy

Try this

Code:
Public Sub ReadFile(ByVal Filename As String)
        Dim tStr As String
        Dim Coord(3) As Single
        Dim SR As TextReader = New StreamReader(Filename) Open File
        Do
            tStr = SR.ReadLine
 
Back
Top