Filestream (C#)

Morpheus

Well-known member
Joined
Jan 18, 2002
Messages
62
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
 
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.
C#:
StreamReader sr = new StreamReader(fs);
string s = sr.ReadLine();

while (s != null)
{
  // do something with the word s
  s = sr.ReadLine()
}
 
I think binary formater is the best way to do it for me. But I cant get it to work.

This is the whole class for the IO.

C#:
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
 
Last edited by a moderator:
I think there is a problem within these lines...

C#:
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.
 
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.
 
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.
 
The file exists so thats not the problem. Text is saved to the file from the method newCustomer() which is included in my code above.
 
Instead of trying to serialize each Customer object, just serialize the entire ArrayList.

C#:
IFormatter f = new BinaryFormatter();
Stream s = new FileStream("C:\file.dat", FileMode.Create, FileAccess.Write, FileShare.None);
f.Serialize(s, alCustomer);
s.Close();

Unless Im missing something this will be drastically easier.
 
I think I have solved that problem. But now I have another problem.

C#:
[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
 
You cant just assume that the index of the last item is equivalent to the the number of items in the ArrayList. Simply put, it just doesnt work that way.

Instead of trying to find the index of the last item why dont 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.
 
Well I solved it in another way but thank you very much!

But I have another problem (again).

I have this method.

C#:
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?
 
Back
Top