reading in tab delimited text into arraylist

G

Guest

I have code right now that reads in the file into an arraylist, but it reads
in each line of the text file as a single element rather than the separate
tab delimited strings in the line. Is there some way to split the text as
its being read in? The file is both tab delimited and has a carriage return
at the end of each line. Thanks!

using System;
using System.IO;
using System.Collections;

namespace try_again
{
class Class1
{
static void Main(string[] args)
{
StreamWriter swa = new StreamWriter ("c:\\afinal.txt");
StreamReader sra = new StreamReader ("c:\\ChristineWork\\test1.txt");
string aLine = "";

ArrayList aColumn = new ArrayList ();
while (aLine != null)
{
aLine = sra.ReadLine();

if (aLine != null)
{
string line = aLine.Split(new Char [] {' '});
aColumn.Add(aLine);

Console.WriteLine(">"+aLine+"<");

}
Console.ReadLine();
sra.Close();
 
D

Dan =o\)

string FileBuffer = sra.ReadToEnd().Replace ( "\r\n", "\t" );
string [] TokenBuffer = FileBuffer.split ( '\t' );
 
G

Guest

Ok, here is the code that I have now. The second console.writeline only
gives the type of TokenBuffer as opposed to the actual values. I have run
into this before, but my memory is failing me when it comes to how I cleared
this up.

StreamWriter swa = new StreamWriter ("c:\\afinal.txt");
StreamReader sra = new StreamReader ("c:\\ChristineWork\\test1.txt");

string aLine = sra.ReadToEnd();
Console.WriteLine(aLine);
string[] TokenBuffer = aLine.Split('\t', '\r');
Console.WriteLine(TokenBuffer);

Console.ReadLine();
sra.Close();


Dan =o) said:
string FileBuffer = sra.ReadToEnd().Replace ( "\r\n", "\t" );
string [] TokenBuffer = FileBuffer.split ( '\t' );


Christine said:
I have code right now that reads in the file into an arraylist, but it
reads
in each line of the text file as a single element rather than the separate
tab delimited strings in the line. Is there some way to split the text as
its being read in? The file is both tab delimited and has a carriage
return
at the end of each line. Thanks!

using System;
using System.IO;
using System.Collections;

namespace try_again
{
class Class1
{
static void Main(string[] args)
{
StreamWriter swa = new StreamWriter ("c:\\afinal.txt");
StreamReader sra = new StreamReader ("c:\\ChristineWork\\test1.txt");
string aLine = "";

ArrayList aColumn = new ArrayList ();
while (aLine != null)
{
aLine = sra.ReadLine();

if (aLine != null)
{
string line = aLine.Split(new Char [] {' '});
aColumn.Add(aLine);

Console.WriteLine(">"+aLine+"<");

}
Console.ReadLine();
sra.Close();
 
J

James Curran

Replace
Console.WriteLine(TokenBuffer);

with

foreach(string token in TokenBuffer)
Console.WriteLine(token);

--
Truth,
James Curran
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
(note new day job!)
Christine said:
Ok, here is the code that I have now. The second console.writeline only
gives the type of TokenBuffer as opposed to the actual values. I have run
into this before, but my memory is failing me when it comes to how I cleared
this up.

StreamWriter swa = new StreamWriter ("c:\\afinal.txt");
StreamReader sra = new StreamReader ("c:\\ChristineWork\\test1.txt");

string aLine = sra.ReadToEnd();
Console.WriteLine(aLine);
string[] TokenBuffer = aLine.Split('\t', '\r');
Console.WriteLine(TokenBuffer);

Console.ReadLine();
sra.Close();


Dan =o) said:
string FileBuffer = sra.ReadToEnd().Replace ( "\r\n", "\t" );
string [] TokenBuffer = FileBuffer.split ( '\t' );


Christine said:
I have code right now that reads in the file into an arraylist, but it
reads
in each line of the text file as a single element rather than the separate
tab delimited strings in the line. Is there some way to split the text as
its being read in? The file is both tab delimited and has a carriage
return
at the end of each line. Thanks!

using System;
using System.IO;
using System.Collections;

namespace try_again
{
class Class1
{
static void Main(string[] args)
{
StreamWriter swa = new StreamWriter ("c:\\afinal.txt");
StreamReader sra = new StreamReader ("c:\\ChristineWork\\test1.txt");
string aLine = "";

ArrayList aColumn = new ArrayList ();
while (aLine != null)
{
aLine = sra.ReadLine();

if (aLine != null)
{
string line = aLine.Split(new Char [] {' '});
aColumn.Add(aLine);

Console.WriteLine(">"+aLine+"<");

}
Console.ReadLine();
sra.Close();
 
G

Guest

Ok this is working great except it is picking up spaces as elements now. I
only want the strings that are not null. How do I do this?

string aLine = sra.ReadToEnd();
string[] TokenBuffer = aLine.Split(new Char [] {' '});
foreach(string token in TokenBuffer)
{
Console.WriteLine(token);
}

Console.ReadLine();
sra.Close();

Console.WriteLine(TokenBuffer.Length);
Console.ReadLine();

James Curran said:
Replace
Console.WriteLine(TokenBuffer);

with

foreach(string token in TokenBuffer)
Console.WriteLine(token);

--
Truth,
James Curran
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
(note new day job!)
Christine said:
Ok, here is the code that I have now. The second console.writeline only
gives the type of TokenBuffer as opposed to the actual values. I have run
into this before, but my memory is failing me when it comes to how I cleared
this up.

StreamWriter swa = new StreamWriter ("c:\\afinal.txt");
StreamReader sra = new StreamReader ("c:\\ChristineWork\\test1.txt");

string aLine = sra.ReadToEnd();
Console.WriteLine(aLine);
string[] TokenBuffer = aLine.Split('\t', '\r');
Console.WriteLine(TokenBuffer);

Console.ReadLine();
sra.Close();


Dan =o) said:
string FileBuffer = sra.ReadToEnd().Replace ( "\r\n", "\t" );
string [] TokenBuffer = FileBuffer.split ( '\t' );


I have code right now that reads in the file into an arraylist, but it
reads
in each line of the text file as a single element rather than the separate
tab delimited strings in the line. Is there some way to split the text as
its being read in? The file is both tab delimited and has a carriage
return
at the end of each line. Thanks!

using System;
using System.IO;
using System.Collections;

namespace try_again
{
class Class1
{
static void Main(string[] args)
{
StreamWriter swa = new StreamWriter ("c:\\afinal.txt");
StreamReader sra = new StreamReader ("c:\\ChristineWork\\test1.txt");
string aLine = "";

ArrayList aColumn = new ArrayList ();
while (aLine != null)
{
aLine = sra.ReadLine();

if (aLine != null)
{
string line = aLine.Split(new Char [] {' '});
aColumn.Add(aLine);

Console.WriteLine(">"+aLine+"<");

}
Console.ReadLine();
sra.Close();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top