Solved

reading tab-separated files in C#

Posted on 2006-07-11
2
1,601 Views
Last Modified: 2008-01-09
Is there a function in C# that reads tab- (or comma-) separated textfiles? For those of you who's familiar with Matlab, what I need is an equivalent of textread. At most, I need to be able to speficy the format of each  line in the file, i.e. the number of fields and their types. I also need to be able to skip a specified number of "header" lines at the beginning of a file.
0
Comment
Question by:ilyaz
2 Comments
 
LVL 7

Expert Comment

by:whatsit2002
ID: 17084373
I don't think you'll find anything built into the .NET framework, but here is an example of what I have done in the past to accomplish something similar:

try
{
      sr = File.OpenText(fileLocation + @"\" + fileName);
      while (sr.Peek() >= 0)
      {
            string[] row = sr.ReadLine().Split('\t');
            ProcessRow(fileName, row);
      }
      sr.Close();
}
catch {}


Where ProcessRow() is a method that uses the filename to figure out what type of record is being passed in (i.e. knows what field matches what index in the string array passed).

For other deliminters, simply pass in the correct character to the Split function.

Hope this helps.

Jason
0
 
LVL 2

Accepted Solution

by:
M3hcSS earned 125 total points
ID: 17084961
You can use the regular expression parser (regex). It works wonderfully for parsing out text files with tabs, commas, etc...

using System.Text.RegularExpressions;

http://www.csharphelp.com/archives/archive21.html

and a quick tutorial on using regex...

http://www.codeproject.com/dotnet/RegexTutorial.asp
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now