EOF not set with StreamWriter  
Author Message
perstam





PostPosted: .NET Base Class Library, EOF not set with StreamWriter Top

Hello,

When I execute the following code the EOF marker is not being set correctly. I've tried flush and that doesn't seem to work.

StreamWriter FPFileOut;

FileStream FavoritesFS;

FavoritesFS = new FileStream("FavoritePrograms.txt",

FileMode.OpenOrCreate, FileAccess.Write);

FavoriteProgramsRecord FPRecord = new FavoriteProgramsRecord();

FPFileOut = new StreamWriter(FavoritesFS);

for (int i = 0; i < FavoritesArray.Count; i++)

FPFileOut.WriteLine(FavoritesArrayIdea);

FPFileOut.Close();

FavoritesFS.Close();

}

Originally my file contains

ABC

Heroes

Jericho

After I programmatically remove Heroes from the FavoritesArray (using FavoritesArray.Remove) leaving only 2 elements (ABC and Jericho) the resulting output file contains

ABC

Jericho

ericho

when I open it in NotePad. As you can see Jericho replaces Heroes, but the remainder of the string of Jericho -- "ericho" remains in the file. I don't want this to happen. I want EOF marker set to the beginning of the file when I open it so that I get

ABC

Jericho

in the file. I have placed VS 2005 in debug mode and verified that Favorites Array contains only 2 elements.

Help would be appreciated.



.NET Development21  
 
 
nobugz





PostPosted: .NET Base Class Library, EOF not set with StreamWriter Top

Change the FileMode argument in the FileStream constructor call to FileMode.Create


 
 
Sean Hederman





PostPosted: .NET Base Class Library, EOF not set with StreamWriter Top

Another appraoch would be to use the FileStream.SetLength to the length you desire. There is no such thing as an EOF marker in Windows, there it simply the size of the file. FileMode.Create will overwrite the old file with the new one, SetLength will drop the size to the desired size.



 
 
perstam





PostPosted: .NET Base Class Library, EOF not set with StreamWriter Top

Thanks so much. What I wanted it to do was overwrite the file so I changed the FileMode to Create.