simon77 Posted January 17, 2005 Posted January 17, 2005 OK, I'm 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 we've got .NET installed and having my first a attempt at forms and dialouge boxes rather than my usual console stuff. Anyway I'm using streamreader to read the file in but can't grab less than a line at a time. StreamReader* objReader = new StreamReader String *sLine = ""; sLine = objReader->ReadLine(); Surely I don't 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 I've googled long and hard for the answer... Quote
Administrators PlausiblyDamp Posted January 17, 2005 Administrators Posted January 17, 2005 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. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
simon77 Posted January 17, 2005 Author Posted January 17, 2005 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. I'll go check out the .Read method. Thanks. Quote
Napivo1972 Posted January 17, 2005 Posted January 17, 2005 Surely I don't 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 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 �read a ine If Not IsNothing(tStr) Then Coord = Coordsplitter(tStr) Loop Until IsNothing(tStr) End Sub Private Function Coordsplitter(ByVal tstr As String) As Single() �Splits the String Dim vCoord(3) As Single Dim t1 As Integer = 0 For Each value As String In Split(tstr, " ", 3) �Split in 3 pieces vCoord(t1) = Convert.ToSingle(value) �Convert to Single t1 = t1 + 1 Next Return vCoord End Function this is the text file I assumed 12.23 13.12 123.32 12.23 13.12 123.33 12.23 13.12 123.34 12.23 13.12 123.35 12.23 13.12 123.36 Hope it helps Quote
simon77 Posted January 17, 2005 Author Posted January 17, 2005 Ahh brilliant. Thanks a lots, thats what I was after. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.