Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to delete content from file?

Status
Not open for further replies.

sarzi

Programmer
Feb 25, 2004
3
US
I have a binary file which contains a header for the size of the whole file and two structures.

{ header (12 bites), struct1, struct2}

I need to delete the struct1 from the file and shift the content of struct2 forward to where struct1 orginally located. After the removal, the file size should be the size of the header + size of struct2

I use memmove, it just COPY struct2 to where struct1 was, however, the content of struct2 still remain where it is. the file looks like (header, struct2, struct2). and the file size remains unchanged.

Can anyone tell me how to move (NOT COPY) the contents of struct2 to struct1?

 
- open a new (temporary) file
- write the bits you want to keep, in the places you want to keep them
- close the new file
- delete the old file
- rename the new file to the original name

--
 
hi sarzi,

you have in memory all you need to do it. How Salem showed you, there is no way to delete a part of a sequential file.

I belive you use read and write routines: follow this flow

open ( read mode)
read( dim= 12 + sizeof(struct1) + sizeof(struct2) )
close file

make operation in memory (memmove, memcpy, what you want)

open ( write mode ) (destroy file)
write ( dim= 12 + sizeof(struct1) )
close

bye

 
Problem with solution 2 (by victorv):

The file may be lost if program or system crashes after opening it the second time (in write mode) and before it is writen. So either use solution 1 or make a copy of the file before over-writing it.
 
hi dsanchez2,
for your opinion, all commercial program as
Word, CAD programs (they write up to 50mb file), DBs, ecc
make a "backup" copy before write a record or a complete file ? I think no ! For rare occasion in which this appens,
backup system are used.
bye
 
You could open in i/o mode,
fread entire file into its parts.
fseek (dim=12)
fwrite(struct2)
ftruncate(fd,dim=12+sizeof(struct2);
fclose.

that is if one record in file, and compiler supports ftruncate.

otherwise easiest to rewrite a new file.

Also if this is unix, then awk could easily do this.
 
Windows-specific solution: use
Code:
int _chsize(int handle, long size);
from <errno.h>.
Save new length, close C-stream, open file via _open() from <io.h> in r/w mode, call _chsize then _close(handle). Make your truncate() equivalent...
 
Victorv,

Most text editors I know (eg Vi) work on a copy of the file. They only update the original file when you save.

What I described can also occur if the user interrupts the program after "destroying" the original file. Are you going to tell the user that they have to recover a file from backups each time they press control-C?

It is always better to be save than sorry. You always program with safety in mind. Backups are there as a last result.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top