943,573 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 7017
  • C# RSS
Feb 12th, 2009
0

Writing ArrayList to textFile?

Expand Post »
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.

C# Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
C#Newbie is offline Offline
7 posts
since Feb 2009
Feb 12th, 2009
0

Re: Writing ArrayList to textFile?

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.
Reputation Points: 11
Solved Threads: 9
Junior Poster in Training
vckicks is offline Offline
58 posts
since Jun 2008
Feb 12th, 2009
0

Re: Writing ArrayList to textFile?

Use output.Write(fr.friendName); output.Write(fr.friendStreet); etc.
Assuming they are strings.
Reputation Points: 2023
Solved Threads: 644
Senior Poster
ddanbe is offline Offline
3,735 posts
since Oct 2008
Feb 12th, 2009
0

Re: Writing ArrayList to textFile?

Click to Expand / Collapse  Quote originally posted by ddanbe ...
Use output.Write(fr.friendName); output.Write(fr.friendStreet); etc.
Assuming they are strings.
Thank you that made sense. However, I changed it to:

C# Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
C#Newbie is offline Offline
7 posts
since Feb 2009
Feb 12th, 2009
0

Re: Writing ArrayList to textFile?

Hello, C#Newbie. Try to add Flush before closing your StreamWriter.

in your case it's:
c# Syntax (Toggle Plain Text)
  1. output.flush();
Last edited by Antenka; Feb 12th, 2009 at 11:49 am.
Reputation Points: 293
Solved Threads: 82
Posting Whiz
Antenka is offline Offline
361 posts
since Nov 2008
Feb 12th, 2009
0

Re: Writing ArrayList to textFile?

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()
Reputation Points: 2023
Solved Threads: 644
Senior Poster
ddanbe is offline Offline
3,735 posts
since Oct 2008
Feb 12th, 2009
0

Re: Writing ArrayList to textFile?

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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
C#Newbie is offline Offline
7 posts
since Feb 2009
Feb 12th, 2009
0

Re: Writing ArrayList to textFile?

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)
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Gisha is offline Offline
1 posts
since Feb 2009
Feb 12th, 2009
0

Re: Writing ArrayList to textFile?

Click to Expand / Collapse  Quote originally posted by Gisha ...
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
C#Newbie is offline Offline
7 posts
since Feb 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: problems compiling first Windows application
Next Thread in C# Forum Timeline: Palindrome test function





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC