Writing ArrayList to textFile?

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

Join Date: Feb 2009
Posts: 7
Reputation: C#Newbie is an unknown quantity at this point 
Solved Threads: 0
C#Newbie C#Newbie is offline Offline
Newbie Poster

Writing ArrayList to textFile?

 
0
  #1
Feb 12th, 2009
Hi. I am working on my Final for my Intro to C# class so yes I am new and my code is pretty unprofessional probably. We are required to use a array or a class to read addresses from a text file and display them in the GUI. We should be able to add new addresses and delete address and sort the arraylist by Name. Ok Everything works except...the reading and writing to the text file and the sorting. I tried the following to write to the text file which I have in the bin/debug folder of the program. Also I am using Visual Studio 2003 .Net Framework1.1 just in case that helps. ok here is the code I don't get any errors but the file is still blank when I open it.

  1. private void menuSave_Click(object sender, System.EventArgs e)
  2. {
  3. // if the file exists
  4. if (File.Exists("AddressList.txt"))
  5.  
  6. {
  7. try
  8. {
  9. // creates a new StreamWriter
  10. StreamWriter output = new StreamWriter(Application.StartupPath + "\\AddressList.txt");
  11. // writes each friend to the text file
  12. foreach(Friend fr in friendArray)
  13. {
  14. output.Write(fr.friendName, fr.friendStreet, fr.friendCity, fr.friendState, fr.friendZip);
  15.  
  16. }
  17. //closes the file
  18. output.Close();
  19. }
  20.  
  21. catch(System.IO.IOException exc)
  22. {
  23. MessageBox.Show(exc.Message);
  24. }
  25. }
  26. else
  27. {
  28. MessageBox.Show("File Not Found");
  29. }
  30.  
  31.  
  32. }
I also tried:
output.Write(" {0}, {1}, {2}, {3}, {4}",fr.friendName, fr.friendStreet, fr.friendCity, fr.friendState, fr.friendZip);

Didnt work either.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 58
Reputation: vckicks is an unknown quantity at this point 
Solved Threads: 9
vckicks vckicks is offline Offline
Junior Poster in Training

Re: Writing ArrayList to textFile?

 
0
  #2
Feb 12th, 2009
Just a quick glance I would play around with this line:

output.Write(fr.friendName, fr.friendStreet, fr.friendCity, fr.friendState, fr.friendZip);

You have to convert the arraylist element into a string or something before writing it to the file. Just make sure it's in a format that you can read back later.
Visual C# Kicks - Free C# code resources and articles.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,956
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 283
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: Writing ArrayList to textFile?

 
0
  #3
Feb 12th, 2009
Use output.Write(fr.friendName); output.Write(fr.friendStreet); etc.
Assuming they are strings.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 7
Reputation: C#Newbie is an unknown quantity at this point 
Solved Threads: 0
C#Newbie C#Newbie is offline Offline
Newbie Poster

Re: Writing ArrayList to textFile?

 
0
  #4
Feb 12th, 2009
Originally Posted by ddanbe View Post
Use output.Write(fr.friendName); output.Write(fr.friendStreet); etc.
Assuming they are strings.
Thank you that made sense. However, I changed it to:

  1. private void menuSave_Click(object sender, System.EventArgs e)
  2. {
  3. // if the file exists
  4. if (File.Exists("AddressList.txt"))
  5.  
  6. {
  7. try
  8. {
  9. // creates a new StreamWriter *****************************************
  10. // ********************************************************************
  11. // ITS NOT WRITING TO THE FILE.
  12. StreamWriter output = new StreamWriter(Application.StartupPath + "\\AddressList.txt");
  13. // writes each friend to the text file
  14. foreach(Friend fr in friendArray)
  15. {
  16. //output.WriteLine("{0}, {1}, {2}, {3}, {4}", fr.friendName.ToString(), fr.friendStreet.ToString(), fr.friendCity.ToString(), fr.friendState.ToString(), fr.friendZip.ToString());
  17. output.WriteLine(fr.friendName.ToString());
  18. output.WriteLine(fr.friendStreet.ToString());
  19. output.WriteLine(fr.friendState.ToString());
  20. output.WriteLine(fr.friendState.ToString());
  21. output.WriteLine(fr.friendZip.ToString());
  22. }
  23. //closes the file
  24. output.Close();
  25. }
  26.  
  27. catch(System.IO.IOException exc)
  28. {
  29. MessageBox.Show(exc.Message);
  30. }
  31. }
  32. else
  33. {
  34. MessageBox.Show("File Not Found");
  35. }
  36.  
  37.  
  38. }
But guess what? The file is still blank! LOL. Any other ideas that I can try? I know the info is stored in the array I can view it add new ones and delete them. Just don't know why they wont write.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 249
Reputation: Antenka has a spectacular aura about Antenka has a spectacular aura about Antenka has a spectacular aura about 
Solved Threads: 65
Antenka's Avatar
Antenka Antenka is offline Offline
Posting Whiz in Training

Re: Writing ArrayList to textFile?

 
0
  #5
Feb 12th, 2009
Hello, C#Newbie. Try to add Flush before closing your StreamWriter.

in your case it's:
  1. output.flush();
Last edited by Antenka; Feb 12th, 2009 at 11:49 am.
So what if you can see the darkest side of me?
No one would ever change this animal I have become
Help me believe it's not the real me
Somebody help me tame this animal
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,956
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 283
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: Writing ArrayList to textFile?

 
0
  #6
Feb 12th, 2009
You are writing to "\\AddressList.txt" instead of "AddressList.txt".
Use Write, use WriteLine if you want to start a new line.
Use the using keyword. That way you don't have to bother to close the stream.
If friendName is already a string you don't have to do friendName.ToString()
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 7
Reputation: C#Newbie is an unknown quantity at this point 
Solved Threads: 0
C#Newbie C#Newbie is offline Offline
Newbie Poster

Re: Writing ArrayList to textFile?

 
0
  #7
Feb 12th, 2009
Oh My God. Just out of curiosity I clicked on the savemenu Item again to see if it goes to that event and guess what. It opened another event. That is so weired since I did the first savemenu Item_click event the same way. Anyhow...It works now....whooo wheeee. Thanks to all of you!
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1
Reputation: Gisha is an unknown quantity at this point 
Solved Threads: 1
Gisha Gisha is offline Offline
Newbie Poster

Re: Writing ArrayList to textFile?

 
0
  #8
Feb 12th, 2009
Hi,

I am Gisha.I am a C# platform programmer.I just want to know the basic steps to create a simple c# application. pls sent me the queries for creation of simple c# application...........


Thank"U"

Regards

(Gisha.K)
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 7
Reputation: C#Newbie is an unknown quantity at this point 
Solved Threads: 0
C#Newbie C#Newbie is offline Offline
Newbie Poster

Re: Writing ArrayList to textFile?

 
0
  #9
Feb 12th, 2009
Originally Posted by Gisha View Post
Hi,

I am Gisha.I am a C# platform programmer.I just want to know the basic steps to create a simple c# application. pls sent me the queries for creation of simple c# application...........


Thank"U"

Regards

(Gisha.K)
You can go here: http://msdn.microsoft.com/en-us/library/a72418yk.aspx

and see if that helps. You can download Visual Studio C# 2008 Express Edition free:
http://www.microsoft.com/eXPress/download/

This should get you started.
Reply With Quote Quick reply to this message  
Reply

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



Other Threads in the C# Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC