Solved

How to read columns of a tab delimited text file.

Posted on 2012-03-16
6
2,836 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

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…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

770 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