Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

reading tab-separated files in C#

Posted on 2006-07-11
2
Medium Priority
?
1,612 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
[X]
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
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 500 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
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
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

722 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