Hello,
I am saving my data in a text file. I can successfully access my file and add records in it and even search it. but when it comes to del a record i dont know how to do it. Can some one please advice.

Regards,
Vishal

If you have a secure erasing application you can get rid of the whole text file. I hope this helps, and tell me if it does.

A couple ways are:

1) include a flag within the data of each record indicating that this record is to be ignored and leave it within the file. Works nice if file storage isn't a concern

2) or keep track of which records have been "deleted" and can be overwritten. Minimizes file space but can be a hassle keeping track of everything and using offsets, etc.

3) but probably the easiest way, if the file isn't too big, is to write the entire file to memory, make the desired deletions, then write the adjusted records back to file overwriting the material read in originally.

4) and I'm sure there are other ways, too.

If you have a secure erasing application you can get rid of the whole text file. I hope this helps, and tell me if it does.

I am sorry i did not get wht you are trying to say.

A couple ways are:

1) include a flag within the data of each record indicating that this record is to be ignored and leave it within the file. Works nice if file storage isn't a concern

2) or keep track of which records have been "deleted" and can be overwritten. Minimizes file space but can be a hassle keeping track of everything and using offsets, etc.

3) but probably the easiest way, if the file isn't too big, is to write the entire file to memory, make the desired deletions, then write the adjusted records back to file overwriting the material read in originally.

4) and I'm sure there are other ways, too.

I like option 3 has the file ain't too big I will execute it and will advice you.. I wish though we could just send a del command insted.

Hi Vishal,
Generally to delete a record from a file. we are having a simple technique. Here we will have two files.
( Ex : (1) "Employee.txt" ------ in this actual data can be stored)
(2)"Temp.text" ---------- it is a temporary file used )

1--> copy all the records except the record which is to be deleted into "Temp.txt" from "Employee.txt"
2-->delete the "Employee.txt" file by using remove function.

3-->Rename the "Temp.txt" to 'Employee.txt" by using rename function.

I think u can understand. I am giving a sample code , just observe it.
another='y';

while(another=='y')
    {
     printf("\nEnter name of employee to delete : ");
     scanf("%s",empname);
     ft=fopen("TEMP.DAT","wb");
     rewind(fp);
     while(fread(&e,recsize,1,fp)==1)
      {
       if(strcmp(e.name,empname)!=0)
       fwrite(&e,recsize,1,ft);
      }
      fclose(fp);
      fclose(ft);
      remove("EMP.DAT");
      rename("TEMP.DAT","EMP.DAT");
      fp=fopen("EMP.DAT","rb+");
      printf("\nDelete another record (y/n) ? : ");
      fflush(stdin);
      another=getche();

Edited 6 Years Ago by Nick Evan: Added code-tags

Comments
Horrible code, no tags and a bump...

Right click the file and select delete.
If it gives you the command "you dont have authority to delete" or "it is write protected" goto properties of that particular file and deselect 'readonly' radio button.
And continue the deleting steps....

Comments
How do you program a "right click"?

Hello,
I am saving my data in a text file. I can successfully access my file and add records in it and even search it. but when it comes to del a record i dont know how to do it. Can some one please advice.

Regards,
Vishal

To delete a record just read all records from the file other than the record which u want to delete and place it in another temporary file. Remove the actual file and rename the temporary file.

@seshendra hari
your code would remove all the records leaving the lastone behind...if you try deleting the lastone it wont work...!!!

This article has been dead for over six months. Start a new discussion instead.