473,396 Members | 2,039 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Best way to read Tab-delimited text file

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

2 37849
xc0de
1
You should check out "http://filehelpers.sourceforge.net/"
Apr 11 '12 #2
PsychoCoder
465 Expert Mod 256MB
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

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

Similar topics

4
by: babylon | last post by:
I created a class which inherits from UserControl.. However, I can't find it in the "My User Controls" tab in designer view... How can i add it back myself? thx...
29
by: pb648174 | last post by:
I have a very long transaction that runs on the same database that other users need to use for existing data. I don't care if they see data from the transaction before it is done and am only using...
2
by: Duncan Booth | last post by:
What is the best way to override the tab order in a web page? I'm trying to implement a zoom mode for part of a form, so when the zoom button is pressed that section of the form is resized to...
3
by: STeve Lefevre | last post by:
Hey folks - I'm working on a form. The form is in datasheet view. There are two fields of concern: "Score" and "Answer". I'm having problems getting the behavior I want out of it. Score and...
1
by: Chris Uwins | last post by:
Hi there, i know theres a number of ways I can achieve this but want to know the best, (but still quite simple). Up until a year ago I never used Access but have designed a few databases for...
9
by: Lance Johnson | last post by:
I would like for when a tab page changes, that my controls on the previous tab page would receive a visiblechanged event or some other event so they are aware of that. We have db locks in some of...
4
by: Marius Trælnes | last post by:
Hello! I am about to make a class/control that creates tabbing functionality. The result is in HTML/CSS/JavaScript output. My question is: Within the tabs I want to simply add user controls...
6
by: Siv | last post by:
Hi, I have a form with a TabControl on it, I have 6 tabs across the top of the page. What I'd like to do is have all but the first and second tab visible when the app starts and then as the user...
5
by: Tom_F | last post by:
To comp.databases.ms-access -- I am trying to re-number the "tab index" for a large number of controls on a form. When I try to do this one control at a time, Access automatically re-numbers...
2
by: Luqman | last post by:
I have a Ajax Tab Container on my Webform with One Panel and some Labels and Textboxes. I want to Iterate through all the textboxes and return their IDs. I tried every possibility I could but...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.