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

How to read columns of a tab delimited text file.

Posted on 2012-03-16
6
2,883 Views
Last Modified: 2012-03-16
I need to read each column value of a tab delimited file.
The file is retrieved.
The column values are not returned.
What do I need to modify?
Thanks,

Source file:
Tab delimited

Date      OldExchange      OldSymbol      NewExchange      NewSymbol
20120315      OTCBB            REFGD      OTCBB            REFG
20120315      OTCBB            SRWY      OTCBB            SRWYD
20120309      OTCBB            ALDFF      AMEX            AXX
20120307      OTCBB            SRCPD      NASDAQ            SRCP

I need to get the column values of each column.

str1 = value of first column "20120315";
str2 = value of 2nd column "OTCBB";
etc.

This is what I have now:
 while ((line = sr.ReadLine()) != null)
        {
            values = line.Split(new char[] { ',' });
            values[values.Length - 1] = values[values.Length - 1].Trim(new char[] { '"' });

           
values[0];
//Returns values[0] as "20120315\tOTCBB\tREFGD\tOTCBB\tREFG"

values[1];
//error Index was outside the bounds of the array.

How do I return the actual column values?
0
Comment
Question by:Dovberman
6 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 125 total points
ID: 37730538
Your logic is splitting on a comma, but you say its a tab-delimited file. Have you tried changing the delimiter for your split?

e.g.

...

values = line.Split(new char[] { '\t' });

...

Open in new window

0
 
LVL 70

Assisted Solution

by:Éric Moreau
Éric Moreau earned 125 total points
ID: 37730690
0
 
LVL 20

Assisted Solution

by:BuggyCoder
BuggyCoder earned 125 total points
ID: 37730781
here is another solution which looks like better and fast in parsing files(like csv)
http://www.codeproject.com/Articles/9258/A-Fast-CSV-Reader

Just see that you change the csv delimiter with \t.

Here is a simple solution:-
public List<string[]> parseCSV(string path)
{
  List<string[]> parsedData = new List<string[]>();

  try
  {
    using (StreamReader readFile = new StreamReader(path))
    {
      string line;
      string[] row;

      while ((line = readFile.ReadLine()) != null)
      {
        row = line.Split(new char[] { '\t' });
        parsedData.Add(row);
      }
    }
  }
  catch (Exception e)
  {
    throw;
  }

  return parsedData;
}

Open in new window

0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

 
LVL 13

Assisted Solution

by:Naman Goel
Naman Goel earned 125 total points
ID: 37730820
Here is code for you used '\t' as deliminator in Spilt method

using System;
using System.IO;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            //read from textfile1.txt
            using (StreamReader sr = new StreamReader("TextFile1.txt"))
            {
                string line = String.Empty;
                string[] values = null;

                while ((line = sr.ReadLine()) != null)
                {
                    values = line.Split(new char[] { '\t' });
                    values[values.Length - 1] = values[values.Length - 1].Trim(new char[] { '"' });
                    // and you will get all columns in values array as expected

                }
            }
        }
    }
}

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37730827
If two more people come in and mention using tab instead of comma for the delimiter, does that mean I get a Yahtzee?
0
 

Author Closing Comment

by:Dovberman
ID: 37731554
The tab delimiter '\t' resolved the issue. Additional detail was also helpful.
Thanks,
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

Suggested Solutions

Title # Comments Views Activity
No Data for DropDown List 2 28
defining NULL or 0 10 44
Could not load file or assembly 'Microsoft.Web.Infrastructure 4 33
asp.net mvc 2 22
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

856 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