Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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?

  • *Experts*
Posted

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()
}

"Being grown up isn't half as fun as growing up

These are the best days of our lives"

-The Ataris, In This Diary

Posted (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 by divil
  • *Experts*
Posted

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.

"Being grown up isn't half as fun as growing up

These are the best days of our lives"

-The Ataris, In This Diary

Posted

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.

  • *Experts*
Posted

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.

"Being grown up isn't half as fun as growing up

These are the best days of our lives"

-The Ataris, In This Diary

  • *Gurus*
Posted

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.

Posted

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.

  • *Gurus*
Posted

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.

Posted

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?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...