How to read a tab delimited file using C# VS 2005

Posted on 2007-10-11
Last Modified: 2008-01-09
I am a newbie to VS 2005 C#. A common task I do in other languages is the reading of tab delimited text files. For this question can you provide a small snippet of code that would illustrate how to open, read and then parse a tab delimited test file? In this case, I also then need to write to a tab delimited text file after some data manipulation. A small snippet of code to write the text data back out to a second text file would be helpful as well.

Thank you,
Question by:rwheeler23
    LVL 5

    Expert Comment

    sorry, I'm too lazy to write the code, just do this:
    open the file using TextReader, loop then read line by line using ReadLine, finish the loop at the EOF.

    In the loop you have a String, loop into it searching for char 9, and thats it, use the substring for your purposes.
    LVL 85

    Accepted Solution

    Some simple examples...

                // to read a tab delimited file...
                String fileName = @"c:\someFile.txt";
                System.IO.StreamReader sr = new System.IO.StreamReader(fileName);
                String[] values;
                String line = sr.ReadLine();            
                while (line != null)
                    values = line.Split('\t');
                    // do something with "values"...

                    line = sr.ReadLine();

                // to write to a file...
                System.IO.StreamWriter sw = new System.IO.StreamWriter(fileName);
    LVL 26

    Expert Comment

    by:Anurag Thakur
    i hope this helps

    using System;
    using System.IO;
    using System.Collections;
    using System.Text.RegularExpressions;

    namespace ReadingFile
          /// Summary description for Class1.
          class Class1
                /// The main entry point for the application.
                static void Main(string[] args)
                      char [] temp = new char[4];
                      StreamReader sr = new StreamReader(File.OpenRead("file.txt"));
                      ArrayList myAL = new ArrayList();
                            while(sr.Peek() != -1)
                      catch(EndOfStreamException e)
                            Console.WriteLine(" the error is" + e);
                      int r =myAL.Count;
                      string[] str;
                      foreach(object o in myAL)
                            str = Regex.Split(o.ToString(), "   ");    
                            System.Collections.IEnumerator myEnumerator = str.GetEnumerator();
                            while ( myEnumerator.MoveNext() )
                                  Console.Write( "\n values are " + myEnumerator.Current );



    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Article by: Najam
    Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
    This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    760 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

    10 Experts available now in Live!

    Get 1:1 Help Now