Reputation: 4359
I need to read in a text file that can range from 8k to 5MB. This file is made up of a single line of text. No Carriage returns or End of Lines. I then need to break it down by to its individual pieces. Those pieces are delimited by size. For example, the first chuck of information is made up of 240 characters. In that 240 characters the first 30 are the Name field. The next 35 are the Address, and so on. Parsing aside, is the StreamReader class the best choice for reading it into memory?
Upvotes: 1
Views: 1305
Reputation: 393934
I'd very much recommend to use a StreamReader as opposed to reading all the text into a string for reasons of heap efficiency. I have had lots of trouble with strings over 2Mb without to much effort (on 32bit .NET).
Do you need further guidance? It seems to me you might be looking for help in treating the stream. It is common for programmers to have more experience in handling strings, and therefore preferring stringy solutions.
If you paste some more specifcs about the structure of the data, I could help you out a bit. For now, just a single general pointer:
All general-purpose parsers and lexers employ a streaming input model. e.g. Look at Coco/C# for a simple to use parser generator.
Upvotes: 0
Reputation: 499382
Look a the TextFieldParser
class, though in the Microsoft.VisualBasic.FileIO
namespace, it can easily be used with C#.
The class description on MSDN is:
Provides methods and properties for parsing structured text files.
An example usage would be:
using(var tfp = new TextFieldParser("path to text file"))
{
tfp.TextFieldType = FieldType.FixedWidth;
tfp.FieldWidths = new int[] {5, 10, 11, -1};
}
Upvotes: 6