By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,665 Members | 1,952 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,665 IT Pros & Developers. It's quick & easy.

Best way to read Tab-delimited text file

P: 7
Hi!

I need to read a text file which has ~10K lines
each line has 7 or 10 rows
each row is separated by TAB

the text file contains some lines which start with "//" and there is no need to store this specific lines.

there is another condition:
case column contains 1 char only and it is 0 or 1 or 2 or 3 or 4, then this will determine the next column's rows count.
if char is 0, 2 or 4 then all next column's row count is 7.
if char is 1 or 3 then all next column's row count is 7.
the row count will change only if there is "end" in the next line.

Also, each multi-row columns' last row is string

- i will need to store all values of all columns and rows in accesible location and i will need to give them names to know where to access to.
e.g. data.positionX and data.positionY are two separate values that exist in every multi-row column

Text file example:
Expand|Select|Wrap|Line Numbers
  1. //blabla
  2. //bla
  3. 1
  4. 2    55    69    14    10    200    //LineDescription
  5. end
  6.  
  7. 3
  8. 2    55    69    14    10    200    55    199    22    //LineDescription
  9. end
  10.  

So, What is the best method to read this file?
Also, can you give me general direction of how to manage to read it?


Thanks in advance!
Mar 9 '12 #1

✓ answered by PsychoCoder

If you're using C# here's an example of reading a delimited file and storing it in a List<String>.

NOTE: If using VB.NET the conversion is very simple

Expand|Select|Wrap|Line Numbers
  1. public static List<string> ReadDelimitedFile(string docPath)
  2. {
  3.     var sepList = new List<string>();
  4.  
  5.     // Read the file and display it line by line.
  6.     using (var file = new StreamReader(docPath))
  7.     {
  8.         string line;
  9.         while ((line = file.ReadLine()) != null)
  10.         {
  11.             var delimiters = new char[] { '\t' };
  12.             var segments = line.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
  13.  
  14.             foreach (var segment in segments)
  15.             {
  16.                 Console.WriteLine(segment);
  17.                 sepList.Add(segment);
  18.             }
  19.         }
  20.  
  21.         file.Close();
  22.     }
  23.     // Suspend the screen.
  24.     Console.ReadLine();
  25.  
  26.     return sepList;
  27. }
  28.  
Hope it can help

Share this Question
Share on Google+
2 Replies


P: 1
You should check out "http://filehelpers.sourceforge.net/"
Apr 11 '12 #2

PsychoCoder
Expert Mod 100+
P: 465
If you're using C# here's an example of reading a delimited file and storing it in a List<String>.

NOTE: If using VB.NET the conversion is very simple

Expand|Select|Wrap|Line Numbers
  1. public static List<string> ReadDelimitedFile(string docPath)
  2. {
  3.     var sepList = new List<string>();
  4.  
  5.     // Read the file and display it line by line.
  6.     using (var file = new StreamReader(docPath))
  7.     {
  8.         string line;
  9.         while ((line = file.ReadLine()) != null)
  10.         {
  11.             var delimiters = new char[] { '\t' };
  12.             var segments = line.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
  13.  
  14.             foreach (var segment in segments)
  15.             {
  16.                 Console.WriteLine(segment);
  17.                 sepList.Add(segment);
  18.             }
  19.         }
  20.  
  21.         file.Close();
  22.     }
  23.     // Suspend the screen.
  24.     Console.ReadLine();
  25.  
  26.     return sepList;
  27. }
  28.  
Hope it can help
Apr 16 '12 #3

Post your reply

Sign in to post your reply or Sign up for a free account.