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();

Recommended Answers

All 5 Replies

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

>> 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

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.

String::Split() apparently has performance issues

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.