cast text file to an array

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Oct 2007
Posts: 10
Reputation: Paua is an unknown quantity at this point 
Solved Threads: 0
Paua Paua is offline Offline
Newbie Poster

cast text file to an array

 
0
  #1
May 17th, 2008
Newbie question;
How do I cast a text file to an array, after the file is read using StreamReader?

I have read many threads, code etc but I really need my hand held here, I just dont get it (especially using windows forms)...this is part of an assignment.... I've been going around in circle's over this for the last week...and I'm a desperate now.

Your help would be appreciated.

this code works as it is, but I don't have an array (not an arraylist cause no manipulation required)


  1. string fileName = "";
  2. string name;
  3. string[] array = new string[8999];//initialize array and size of (NOT USED)
  4.  
  5. //int Count = 0;//set count to 0 to interate through file line by line for linear search
  6.  
  7. OpenFileDialog OpenFile = new OpenFileDialog();//declare variable
  8. OpenFile.ShowDialog(); //show dialog box of files
  9. OpenFile.Filter = "Text files(*.txt)|*.txt|All files (*.*) | *.*"; //filter by these file
  10. types
  11. OpenFile.FilterIndex = 0;//show first file type?
  12.  
  13. fileName = OpenFile.FileName;//declare variable
  14.  
  15. if (OpenFile.ShowDialog() == DialogResult.OK)//if file found then display results to screen
  16. {
  17. foreach (string content in OpenFile.FileNames)
  18. {
  19. TextReader sr = new StreamReader(content);//read file using streamreader
  20.  
  21.  
  22. while ((name = sr.ReadLine()) != null)//read textreader variable line by line
  23. {
  24. listBox1.Items.Add(name);
  25.  
  26. }
  27. sr.Close();
  28. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 10
Reputation: Paua is an unknown quantity at this point 
Solved Threads: 0
Paua Paua is offline Offline
Newbie Poster

Re: cast text file to an array

 
0
  #2
May 17th, 2008
does some-one know what I'm doing wrong here?

  1. foreach (string content in OpenFile.FileNames)//sends opened file to variable.
  2. //{
  3.  
  4. TextReader sr = new StreamReader(content);//supposed to read file via StreamReader
  5. string contents = sr.ReadToEnd();
  6. string[]myArray = (sr);
  7. foreach (string gotya in myArray )
  8. {
  9. listBox1.Items.Add(gotya);
  10. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 10
Reputation: Paua is an unknown quantity at this point 
Solved Threads: 0
Paua Paua is offline Offline
Newbie Poster

Re: cast text file to an array

 
0
  #3
May 17th, 2008
Yeah...Worked it out!, most of the stuff I discovered right here in DaniWeb...all that after scolling the net for the past...ages(I won't give actual time spent going around in circles trying to work it out prior to coming to DaniWeb, way too embrassing).
Well now on to part 2, 3 and documentation OOPs.

so, after all that here it is

  1. TextReader sr = new StreamReader(fileName);
  2.  
  3. string contents = sr.ReadToEnd();
  4. string[]myArray = {contents};
  5. Array.Sort(myArray);
  6. foreach (string gotya in myArray)
  7. {
  8. txtBDisplayContents.Text = gotya;
  9.  
  10. }
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC