Hello how to read from a file,which contains only nimbers(int) and to record them in an array.It's sounds easy,but i can't do it:(

for file example:
1234
4353
2345
2345
2312
1267

I mean they are in collom.
Thanks in advance.

Recommended Answers

All 3 Replies

Try it this way:

string path = @"C:myFolder\myFile.txt";
            List<int> list = new List<int>();
            using (StreamReader sr = new StreamReader(path))
            {
                string line;
                while ((line = sr.ReadLine()) != null)
                    list.Add(Convert.ToInt32(line));
            }

            //generic list<T> now has all the numbers in the collection! 
            //generic list is better then a simple array (int[]).

Sorry ,but can you type it with array

change line 9 (the blank one) to

int[] myIntArray = list.ToArray();
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.