Morpheus Posted February 16, 2003 Posted February 16, 2003 Filestream I need a method that reads from a file and add words to a arraylist. First I need to have a fixed length on the words don´t I? But I can't find a good way to add the words to the arraylist. Could someone please help me? Quote
*Experts* Bucky Posted February 16, 2003 *Experts* Posted February 16, 2003 No, the words do not have to be fixed length. If you put one word on each line in the file, then you can create a StreamReader to read the FileStream, use its ReadLine method to read the stream one line at a time, and for each line add that text to the ArrayList. In this example, fs represents your FileStream. StreamReader sr = new StreamReader(fs); string s = sr.ReadLine(); while (s != null) { // do something with the word s s = sr.ReadLine() } Quote "Being grown up isn't half as fun as growing up These are the best days of our lives" -The Ataris, In This Diary
Morpheus Posted February 17, 2003 Author Posted February 17, 2003 (edited) I think binary formater is the best way to do it for me. But I can't get it to work. This is the whole class for the IO. using System; using System.IO; using System.Collections; using System.Runtime.Serialization.Formatters.Binary; namespace KundReg { /// <summary> /// Summary description for Openfile. /// </summary> public class Openfile { public Openfile() { fileName = "customer.dat"; } protected string fileName; public string FileName { get { return fileName; } set { fileName = value; } } public ArrayList getCustomer() { Customer customer; ArrayList alCustomer = new ArrayList(); FileStream fil = new FileStream(fileName, FileMode.Open); BinaryFormatter bf = new BinaryFormatter(); while(true) { customer = (Customer)bf.Deserialize(fil); alCustomer.Add(customer); } fil.Close(); Console.WriteLine(alCustomer.Count); return alCustomer; } public void newCustomer(Customer customer) { FileStream fil = new FileStream(fileName, FileMode.Append); BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(fil, customer); fil.Close(); } } } Customer is a struct with firstname, lastname etc Sorry about the tabs Edited February 17, 2003 by divil Quote
*Experts* Bucky Posted February 17, 2003 *Experts* Posted February 17, 2003 I think there is a problem within these lines... while(true) { customer = (Customer)bf.Deserialize(fil); alCustomer.Add(customer); } Since the loop runs while true, the loop will run forever until an error occurs. You need to provide a means to exit the loop. You could either put a try-catch statement to catch an error and exit the loop, or something similar. Perhaps you should check if customer is something other than null before adding it to the array list. If it is null, then exit the loop. Quote "Being grown up isn't half as fun as growing up These are the best days of our lives" -The Ataris, In This Diary
Morpheus Posted February 17, 2003 Author Posted February 17, 2003 This was just for a test. I have tried with try catch. And I get this error in the while loop: An unhandled exception of type 'System.Runtime.Serialization.SerializationException' occurred in mscorlib.dll Additional information: Binary stream does not contain a valid BinaryHeader, 0 possible causes, invalid stream or object version change between serialization and deserialization. This occurs att the first loop. Quote
*Experts* Bucky Posted February 18, 2003 *Experts* Posted February 18, 2003 How was this file created? You need to create the customer.dat file by serializing some Customers and saving that. Then you can deserialize it back into your app. Quote "Being grown up isn't half as fun as growing up These are the best days of our lives" -The Ataris, In This Diary
Morpheus Posted February 18, 2003 Author Posted February 18, 2003 The file exists so that's not the problem. Text is saved to the file from the method newCustomer() which is included in my code above. Quote
*Gurus* Derek Stone Posted February 18, 2003 *Gurus* Posted February 18, 2003 Instead of trying to serialize each Customer object, just serialize the entire ArrayList. IFormatter f = new BinaryFormatter(); Stream s = new FileStream("C:\file.dat", FileMode.Create, FileAccess.Write, FileShare.None); f.Serialize(s, alCustomer); s.Close(); Unless I'm missing something this will be drastically easier. Quote Posting Guidelines
Morpheus Posted February 18, 2003 Author Posted February 18, 2003 Hmm I have never really understood interface. Why do you use it in this case? Quote
Morpheus Posted February 19, 2003 Author Posted February 19, 2003 I think I have solved that problem. But now I have another problem. [serializable] public struct Customer { public string firstname; public string lastname; public string address; public string email; public int userID; } The value of these fields is serialized and saved to I file. The program should calculate userID and for this I have made the method newID() which call the method getCustomer() which deserialize the file and put all the fields in a arraylist. The problem is that I don´t know how to access the integer (userID). I know that userID always is the last post in the field so I tried this: int lastIndex = myAl.Count(); int newID = Convert.ToInt32(myAl[index - 1]); But that didn't work I got invalidCastException I think. Quote
*Gurus* Derek Stone Posted February 19, 2003 *Gurus* Posted February 19, 2003 You can't just assume that the index of the last item is equivalent to the the number of items in the ArrayList. Simply put, it just doesn't work that way. Instead of trying to find the index of the last item why don't you try storing it? The Add method of an ArrayList returns the index of the newly added element. Store this when you add the last value, so you can use it when you need it. Quote Posting Guidelines
Morpheus Posted February 20, 2003 Author Posted February 20, 2003 Well I solved it in another way but thank you very much! But I have another problem (again). I have this method. public ArrayList getCustomer() { Customer customer; ArrayList alCustomer = new ArrayList(); FileStream fil = new FileStream(fileName, FileMode.Open); BinaryFormatter bf = new BinaryFormatter(); try { while(true) { customer = (Customer)bf.Deserialize(fil); alCustomer.Add(customer); } } catch(Exception e) { } finally { fil.Close(); } return alCustomer; } I need an identical method, the only difference is that instead of Customer I want Supplier. Do I have to copy the method or is there a better way? Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.