Solved

reading tab-separated files in C#

Posted on 2006-07-11
2
1,599 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

708 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

16 Experts available now in Live!

Get 1:1 Help Now