a list in a txt file. i want to read that file into an array list.
the file contains to colums separated by a tab

how can i do this?

Recommended Answers

All 9 Replies

c# is amazing at doing this, and its all built in - what do you have so far?

private ArrayList addToList()
        {
            ArrayList myList = new ArrayList();
            StreamReader sr = new StreamReader(list);

            for (int i = 0; i < myList.Capacity; i++)
            {
                sr.Read(myList.Add);
            }
            return myList;
        }

this is what i have so far. but it is not working.

Ok, while I know why its not working, explain what you expected that to do, and what you got instead.

i want to add the list in the txt file into the arraylist.

So basically i want it to read line by line and put the contents into the arraylist.

and you missed the part where I asked what you got instead - Im not going to write the code, Im trying to get you to work out why what you wrote is wrong.

not asking you to write my code. just asking for help. i am stuck. this is what i get.

No overload for method 'Read' takes '1' arguments.

now how can i fix it.

The problem is the thing you asked it to read, try it in 2 steps not 1.

I wrote this code for you and it works base on the text file(test.txt) i tested with this content:

row1col1 row1col2 row1col3
row2col1 row2col2 row2col3
row3col1 row3col2 row3col3

note: as you mentioned i seperated columns with TAB

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Collections;

namespace textfromfiletoarraylist
{
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList arrlist = new ArrayList();
            File.OpenRead("test.txt");
            string s = File.ReadAllText("test.txt");

            string[] splits = s.Split(new char[] { '\t' });
            foreach (string str in splits)
            {
                arrlist.Add(str);
            }

            foreach (string st in arrlist)
            {
                Console.WriteLine(st);
            }

            Console.ReadLine();
        }
    }
}

Touraj Ebrahimi [toraj_e] [at] [yahoo] [dot] [com]

Just curios... Do you mean that the array's dimension is the number of columns in the text file? if it is... is the number of column in your text file were all the same? or you just mean a single dimensional array only...

Also to the given code by toraj58, I think there's something missing...

//From
string[] splits = s.Split(new char[] { '\t' });

//To
string[] splits = s.Split(new char[] { '\t',  '\r', '\n' });
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.