i have a text file which contains a single line like 1,2,3,1,2. how do i read this text file and move integers to an array such that array contains only 12312.

Recommended Answers

All 8 Replies

What have you got so far for yourself?
Did you consult MSDN? Example

while ((line = sr.ReadLine()) != null)
{
    string[] values = line.Split(new string[] {","}, StringSplitOptions.None); 

    for (int i = 0; i < values.Length; i++)
    {
    .......
    }

i dont know how to add values to the array

I would do something like this:
List<int> myInts = new List<int>();
then in your for loop do:
myInts.Add(Convert.ToInt32(values[i]));
And if you really want an integer array, you can always use the ToArray method of the List class.
Hopeit helps!

while ((line = sr.ReadLine()) != null)
{
string[] values = line.Split(new string[] {","}, StringSplitOptions.None);
for (int i = 0; i < values.Length; i++)
{
.......
}

i dont know how to add values to the array

I'm little confused here.
Why you need to add value again to the array? You already read text file and store it into array named values.

OP's probably referring to having an array of int's not string's. something like this might work:

        //declare list of ints
        List<int> test = new List<int>();
        //build file
        System.IO.StreamWriter sw = new System.IO.StreamWriter("Test.txt");
        sw.WriteLine("1,2,3,4,5");
        sw.WriteLine("6,7,8,9,0");
        sw.Close();
        //initiate reading from the file
        System.IO.StreamReader sr = new System.IO.StreamReader("Test.txt");
        //loop through each line of the file
        while (!sr.EndOfStream)
        {
            //read a line of the file. split the line into a temporary array using `,` as delimiter, then loop through
            //the array.
            foreach (string s in sr.ReadLine().Split(','))
            {
                //convert each string to an int and add it to the list
                test.Add(Int32.Parse(s));
            }
        }
        sr.Close();
        //a test loop to show the values in the list
        foreach (int I in test)
            listBox1.Items.Add(I.ToString());

Dopn't be put off by using a List. Lists are basically dynamic arrays, and with the ability to add and remove items with one command, much easier to use in many respects. Also as was mentioned if you need to pass it as an array you can use the ToArray() method.

string str= File.ReadAllText(path);
int[] result=new int[5];
for (int i = 0; i < str.Split(',').Length; i++)
{
result[i] = Convert.ToInt32(str.Split(',')[i]);
}

Now i need to read this array.Suppose it has values 131221 and if 1 means A, 3 means B and 1221 means E..then i need to display that file contains one A, one B and one E.
If it reads 112213 then it should display on A,one E and one B.
How should i do this?

That's a total new question, why don't you pose this in a new thread and mark this thread as solved?

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.