newbie: filehandling

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();
 
M

Mohamoss

Hi
What kind of exception do you got and what is its message , you need
to know that in order to know where to start . may be you wrap your code by
try and catch blocks , catch the exception and display it message also know
what part of the code raise the exception here you are some links that
discusses dealing with binary files in C#
http://codenewbie.com/tutorials/1852.html
http://builder.com.com/5100-6389_14-5062324-1-1.html
http://builder.com.com/5100-6389_14-5062324-2.html
Hope that helps
Mohamed Mahfouz
MEA Developer Support Center
ITworx on behalf of Microsoft EMEA GTSC
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

In what line you are getting the exception?


another VERY IMPORTANT thing , in c# char is a 16 bit unicode, not a 8 bit ,
so you better use byte instead of char,
in fact this maybe your problem.

cheers,
 
D

dthom

Well i get an exception when the i get to the binarywriter() - something
like it cannot initiate filesystem.

Somehow i cannot use BinaryReader() and BinaryWriter() in the same
function - cant really understand WHY i need two classes to perform on a
file ?

In C i would do this - very simple (pseudocode):

file=fopen ("filename);
*p=fread (file,wholefilesize);
memmove (p,p+80,wholefilesize-80);
fwrite (file, p, wholefilesize-80);
fclose (file);

Apparently this takes alot of extra lines in c-sharp or ?

What i find hard with this c-sharp, is that there seems to be trillions of
ways of handling files - and im not really sure which way is the right one ?
 
M

Morten Wennevik

dthom,

It would really help if you can provide us with the exact error message and possibly a stack trace.
However, if you aim to remove a byte from a given position, this code should do just that:

public void DeleteRecord(long recnr)
{
FileStream fs = File.Open(filename, FileMode.Open);
byte[] data = new byte[fs.Length];
fs.Read(data, 0, data.Length);
fs.Close();

fs = File.Open(filename, FileMode.Create);
fs.Write(data, 0, (int)recnr);
fs.Write(data, (int)++recnr, data.Length - (int)recnr);
fs.Close();
}

There are probably better ways, and you can use the same stream to write to the same file without closing and recreating, but I'm not sure how to make it clear the data already inside before writing.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top