473,320 Members | 2,092 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

reading in tab delimited text into arraylist

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();

Nov 16 '05 #1
5 15107

string FileBuffer = sra.ReadToEnd().Replace ( "\r\n", "\t" );
string [] TokenBuffer = FileBuffer.split ( '\t' );
"Christine" <Ch*******@discussions.microsoft.com> wrote in message
news:2A**********************************@microsof t.com...
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();


Nov 16 '05 #2
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)" wrote:

string FileBuffer = sra.ReadToEnd().Replace ( "\r\n", "\t" );
string [] TokenBuffer = FileBuffer.split ( '\t' );
"Christine" <Ch*******@discussions.microsoft.com> wrote in message
news:2A**********************************@microsof t.com...
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();


Nov 16 '05 #3
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" <Ch*******@discussions.microsoft.com> wrote in message
news:49**********************************@microsof t.com... 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)" wrote:

string FileBuffer = sra.ReadToEnd().Replace ( "\r\n", "\t" );
string [] TokenBuffer = FileBuffer.split ( '\t' );
"Christine" <Ch*******@discussions.microsoft.com> wrote in message
news:2A**********************************@microsof t.com...
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();


Nov 16 '05 #4
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" wrote:
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" <Ch*******@discussions.microsoft.com> wrote in message
news:49**********************************@microsof t.com...
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)" wrote:

string FileBuffer = sra.ReadToEnd().Replace ( "\r\n", "\t" );
string [] TokenBuffer = FileBuffer.split ( '\t' );
"Christine" <Ch*******@discussions.microsoft.com> wrote in message
news:2A**********************************@microsof t.com...
>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();
>


Nov 16 '05 #5
"Christine" <Ch*******@discussions.microsoft.com> wrote in message
news:48**********************************@microsof t.com...
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?


foreach(string token in TokenBuffer)
{
string token2 = token.Trim();
if (token2.Length > 0)
{
Console.WriteLine(token2);
}
}
--
Truth,
James Curran
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
(note new day job!)
Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Job Lot | last post by:
I have tab delimited text file which gets populated on daily basis via automated process. New entry is written at the bottom. I need to create a utility which makes a copy of this file with 10 most...
1
by: John B. Lorenz | last post by:
I'm attempting to write an input routine that reads from a comma delimited file. I need to read in one record at a time, assign each field to a field array and then continue with my normal...
1
by: Flack | last post by:
Hello, When the user of my app selects some rows from an open excel file and drag-n-drops them onto my form, I need to be able to parse those lines and create a datatable that would match the...
21
by: JoKur | last post by:
Hello, First let me tell you that I'm very new to C# and learning as I go. I'm trying to write a client application to communicate with a server (that I didn't write). Each message from the...
1
by: svijay | last post by:
hi I have got a strange problem. May I know any solution for this. Here is the detailed description about the problem We have got a mainframe system and also production and development...
13
by: ACC | last post by:
Hi! I'm developing an application to analyze some information that is inside a text file. The size of the text file goes from 50Mb to 220Mb... The text file is like a table, with "rows" and...
15
by: VMI | last post by:
I'm parsing a comma-delimited record but I want it to do something if some of the string is between "". How can I do this? With the Excel import it does it correct. I'm using String.Split()....
6
by: =?Utf-8?B?UmljaA==?= | last post by:
'--this code works but only reads text into one column when contains multiple cols Dim ds1x As New DataSet Dim ConStr As String = _ "Provider=Microsoft.Jet.OLEDB.4.0;Data...
2
by: tshad | last post by:
I have a program that is reading a csv file into a dataset. I want it to read the 1st line as data. But it ignores it. I have the Connection set up as: OleDbConnection csvConnection = new...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.