Solved

How to read columns of a tab delimited text file.

Posted on 2012-03-16
6
2,935 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
[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
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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.

756 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