D
dthom
Hi i have a stupid problem here, and im starting to go a little bit nuts.
I have a binary file, consisting of 80 bytes each - what i want to do is to
go into this file, and delete one record, and rewrite the file.
My idea was to read the whole file, do the delete in memory (as shown in my
code) - and then rewrite the file with the record deleted.
But i get exceptions, and it doesnt work.
I thought i could use the binaryreader for this only, but apperently i need
to use binarywriter also ? when im doing that, then i get the exception of
using two "fs" classes.
what am i doing wrong ?? is it the wrong way to do such a thing in csharp ?
im from a C background, and here its simple handling binary files.
Hope you can help me, thanx.
------------------------------------
This is the function:
----------------------------
public void DeleteRecord (long recnr)
long filesize;
FileInfo info = new FileInfo(this.filename);
filesize = info.Length;
char[] wholefile = new char [filesize];
// read the file
FileStream fs = new FileStream(this.filename, FileMode.Open);
BinaryReader r = new BinaryReader(fs);
r.BaseStream.Seek (0, 0);
wholefile = r.ReadChars ((int)filesize);
r.Close();
fs.Close();
// delete the rec in memory file ( 80 bytes)
Array.Copy
(wholefile,(int)((recnr+1)*80),wholefile,(int)((recnr)*80),(int)filesize-1);
// rewrite the file
FileStream fs2 = new FileStream(this.filename, FileMode.Create);
BinaryWriter w = new BinaryWriter(fs2);
w.BaseStream.Seek (0, 0);
w.Write (wholefile,0,(int)(filesize-80));
w.Flush ();
w.Close();
fs2.Close();
I have a binary file, consisting of 80 bytes each - what i want to do is to
go into this file, and delete one record, and rewrite the file.
My idea was to read the whole file, do the delete in memory (as shown in my
code) - and then rewrite the file with the record deleted.
But i get exceptions, and it doesnt work.
I thought i could use the binaryreader for this only, but apperently i need
to use binarywriter also ? when im doing that, then i get the exception of
using two "fs" classes.
what am i doing wrong ?? is it the wrong way to do such a thing in csharp ?
im from a C background, and here its simple handling binary files.
Hope you can help me, thanx.
------------------------------------
This is the function:
----------------------------
public void DeleteRecord (long recnr)
long filesize;
FileInfo info = new FileInfo(this.filename);
filesize = info.Length;
char[] wholefile = new char [filesize];
// read the file
FileStream fs = new FileStream(this.filename, FileMode.Open);
BinaryReader r = new BinaryReader(fs);
r.BaseStream.Seek (0, 0);
wholefile = r.ReadChars ((int)filesize);
r.Close();
fs.Close();
// delete the rec in memory file ( 80 bytes)
Array.Copy
(wholefile,(int)((recnr+1)*80),wholefile,(int)((recnr)*80),(int)filesize-1);
// rewrite the file
FileStream fs2 = new FileStream(this.filename, FileMode.Create);
BinaryWriter w = new BinaryWriter(fs2);
w.BaseStream.Seek (0, 0);
w.Write (wholefile,0,(int)(filesize-80));
w.Flush ();
w.Close();
fs2.Close();