简体   繁体   中英

Text Parsing Tab Delimited file

I have a method that reads a file. This file has roughly 30000 lines. However when I read it into an array I get a random length for my array. I have seen it as low 6000.

I used both

string[] lines = System.IO.File.ReadAllLines(@"C:\\out\\qqqqq.txt");

and

System.IO.StreamReader file = new System.IO.StreamReader(@"C:\\out\\qqqqq.txt");

(and use a counter.)

But I get the same result. I can see in Excel these are too small.

If the line endings in the file are inconsistent (sometimes \\n , sometimes \\r\\n and sometimes \\r ) then you could try reading the entire file as a string and splitting it yourself:

string file = System.IO.File.ReadAllText(@"C:\out\qqqqq.txt");
var lines = file.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);

For large files, this is inefficient, because it needs to read the entire file - using StreamReader you would be able to read the file line-by-line as you're processing it. If performance is an issue, then you could write simple tool that first corrects the line endings.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM