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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Arrays of Structures/Deleting

Status
Not open for further replies.

gonzilla

Programmer
Apr 10, 2001
125
0
0
US
Hi, -

I tried this question in another C forum and didn't get a response. Any help would be great.

=====================================================

The program that I have started to write opens a file and reads in structure formatted binary input:

if ((infile = fopen("flights.dat", "a+b")) == NULL)
exit(EXIT_FAILURE);
rewind(infile);

while (ct < MAXSEATS && fread(&plane[ct], bufsize, 1, infile) == 1)
ct++;

No problem here, it reads them in fine. I have a function that adds new ones and one that deletes them. I would like to be able to write/rewrite them to random places and delete them if needed and replace it with a new one. It seems if I delete it, it never is removed from the file even if I use fwrite like:

scanf(&quot;%d&quot;, &del_seat);
plane[del_seat - 1].status = 0;
fwrite(&plane[del_seat - 1], bufsize, 1, infile);

Is this only possible with linked lists? If it can be done with fwrite, where do I point it's first argument to? And, should I open it in &quot;w+b&quot; instead of &quot;a+b&quot; mode?

Any help or resources would be greatly appreciated.

Thanks.

-Tyler
 
hi Tyler,
for deletions etc,i suggest u open the file with r+b mode.then for changing the i'th record(say), u use the fseek function to make the file pointer point to the desired record.Then u can write the desired rec there with fwrite.For making the ptr point to the i'th rec, u can use fseek(fp,i * sizeof(struct <your struct>),SEEK_SET).this sets the file pointer(fp here).best of luck
-- neo13
 
Thanks neo13.

I have gotten it to work now. The program I'm writing is for class and the specs don't even call for writing to a file, but I always like to go further. One more question.

Assume there are 5 records in there, and each record is 60 bytes (that's my sizeof (structure)) so the file is 300 bytes. There is a struct member in there called plane.status which I set to 0 when deleted.

My question: is it ok to to leave the 60 bytes in the file for the deleted record? Would it be better to get rid of the record completely and if so, what do I fwrite to the file to NULL it out? I have tried zeros, but that doesn't seem to work. Plus, if I have a sequential file, and I delete plane[3].status, that will leave a gap in the file and it will still be the same length.

Thanks.

-Tyler
 
hi Tyler,
yes,when writing to a file,deletion of records is a problem,since u cannot &quot;NULLIFY&quot; a part of the file(like the record in your case).so when u &quot;delete&quot; a record in ur file,u can not remove it unless u move the following data in the file.however such data movement is not a very good idea.so what u can do is leave the rec marked &quot;deleted&quot;(.space=0 in ur case) and the next time u need to insert a record,use this deleted space instead of writing a new rec at the end of the file and increasing the size of the file.i hope this helps.bye
-- neo13
 
Hey,

What does &quot;asdfafafa&quot; mean? Or was that just an accident.

Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top