Click here to Skip to main content
15,891,908 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a text file called FINAL.TXT on my desktop...values in it are
123456

i want to copy these values into an array using stream reader...

i have written a piece of code...but it giving me wrong values...

i have taken a for with button and listbox in it...

here is the code..

button_click event
{

using (FileStream ar1=File.OpenRead(FINAL.TXT))
           {
               using (TextReader ART1 =new  StreamReader(ar1))
               {

                  
                           int i;
                           for (i = 0; i < 6; i++)
                           {
                               array1[i] = Convert.ToInt32(ART1.Read());
                               listBox1.Items.Add(Convert.ToInt32(array1[i]));
                           }
                      

                   
               }
           
           }



}
Posted
Updated 26-Feb-13 17:58pm
v2

Otherwise read like this. Here you can read a single character in loop till it reaches EOF.
C#
using (StreamReader reader1 = File.OpenText(@"final.txt"))
    {
        do
        {
             char next = (char)reader1.Read();
             Console.WriteLine(next);
        }
        while(!reader1.EndOfStream);
    }
 
Share this answer
 
v3
Try a different approach...
C#
string theFileContentsAsString = File.ReadAllText("final.txt");
            int[] theStringAsArray = theFileContentsAsString.ToCharArray().Select(c => Convert.ToInt32(c.ToString())).ToArray();
            //bind theStringArray to listbox here
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900