Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

reading tab-separated files in C#

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

828 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