Hello

I have this code below where I use the method ->ReadToEnd() to read in the whole file into the String test.

The thing is that I have to use the method ->ReadToEnd() in this case because I use another class that only have this functionality. I cant read line by line with that class.

The file I red consists of lines and are 60MB large. If I would read this file with StreamReader line by line, it takes exactly 0.2 seconds. Speed is important in this case.

How is it possible to read the String^ test line by line into the List<String^> getLines as fast as possible?

String^ filePath = "D:\\Folder1\\myTestFile.txt";
            StreamReader^ rd = gcnew StreamReader(filePath);
            List<String^> getLines = gcnew List<String^>();
            String^ test = rd->ReadToEnd();
            rd->Close();

Edited 5 Years Ago by Jennifer84: n/a

ReadToEnd() will not read into that String^ vector. It requires a buffer of contiguous memory. If you want to read that file line-by-line then use the example in the link below.

Example here

Edited 5 Years Ago by Ancient Dragon: n/a

>> If you want to read that file line-by-line

This is what I dont want to do.
In my code below when I read the file into String^ test, the MessageBox shows this:

Line1
Line2
Line3

So the the ReadToEnd() does read all the contents into String^ test and also seems to see the "\n" here to separate it by lines.

Now I need to in somehow read this String^ test as fast as possible into a List<String^>

Notice again now. With the API I have, I do have the oppurtunity to read line by line from the file but this takes 6 seconds for this 60 MB file I have. With ReadToEnd() it takes only 2 seconds.

Is is possible to read the String^ test for example with StreamReader as StreamReader is very fast, can I convert String^ test to any kind of Stream and use StreamReader to read it?

String^ filePath = "D:\\Folder1\\myTestFile.txt";
StreamReader^ rd = gcnew StreamReader(filePath);
List<String^> getLines = gcnew List<String^>();
String^ test = rd->ReadToEnd();
rd->Close();

MessageBox::Show(test);

ReadToEnd() will not read into that String^ vector. It requires a buffer of contiguous memory. If you want to read that file line-by-line then use the example in the link below.

Example here

Edited 5 Years Ago by Jennifer84: n/a

Is the String^::split method available in your API? -- you could split on '\n' and that will fill your array automatically.

String::Split() apparently has performance issues
http://msdn.microsoft.com/en-us/library/b873y76a.aspx#

The Split methods allocate memory for the returned array object and a String object for each array element. If your application requires optimal performance or if managing memory allocation is critical in your application, consider using the IndexOf or IndexOfAny method, and optionally the Compare method, to locate a substring within a string.

Edited 5 Years Ago by Ancient Dragon: n/a

String::Split() apparently has performance issues

Wasn't aware. That's good to know.

Edited 5 Years Ago by jonsca: I'll get those smilies if it's the last thing I do

This article has been dead for over six months. Start a new discussion instead.