In this tutorial we are going to create a file and copy 3 mac ID of 8 byte each as data .Then we call a function to delete a particular entry from the file.

In the main() we do 2 things

  1. write 3 dummy mac id to file
  2. call the delete fn to remove 2nd mac id from the file
#include "stdio.h"
#include "string.h"
#include "stdbool.h"
int delete(char*src,unsigned char*);
int main ()
{
unsigned char arr[8]={0x10,0x08,0x0A,0xF5,0x34,0xBC,0x1E,0x7D};
unsigned char brr[8]={0x80,0x5A,0x47,0xFB,0xC5,0x4D,0x6E,0x31};
unsigned char crr[8]={0x10,0x31,0x08,0x80,0xF5,0x4D,0x0A,0x6E};
FILE *fp;
 fp=fopen("/home/thejas/Desktop/newTest","w");
if(!fp)
 {
printf("file open error\n");
return -1;
 }
fwrite(arr,8,1,fp);
fwrite(brr,8,1,fp);
fwrite(crr,8,1,fp);
fclose(fp);
//fn for calling delete the data
 delete("/home/thejas/Desktop/newTest",brr);
return 1;
}

the file is created with 3 entry

newTest” file

Screenshot from 2016-12-25 21:00:41.png

The fn has argument file location ,data

The delete() function do two major things
1.open desired file
2.compare each mac id and write everything except data to be deleted to a new file and delete old file

int delete(char*src,unsigned char drr[8])
 {
 //create two streams
 FILE *fp,*tmp_fp;
 //temp array for comparing
 unsigned char temp[8]={0};
 //flag for found or not found
 bool found;
 fp=fopen(src,"r");
 tmp_fp=fopen("_tmp","w");
 if(!tmp_fp)
 {
 printf("tmp file open error\n");
 return -1;
 }
 if(!fp)
 {
 printf("file open error {delete()}\n");
 return -2;
 }
 //compare the data
 while(fread(temp,8,1,fp)!=NULL)
 {
 if(strncmp(&temp[0],&drr[0],8)==0)
 {
 printf("item found and deleting\n");
 found=1;
 }
 else
 //save data to temp file
 fwrite(temp,8,1,tmp_fp);
 }
 if(!found){
 printf("Item not found\n");
 return 0;
 }
 fclose(fp);
 fclose(tmp_fp);
 remove(src);
 //rename temp file
 rename("_tmp",src);
 return 0;
 }
  • fp,tmp_fp : two streams for opening files
  • found : flag for print whether the item to be deleted is exist in the file
  • temp[8] :array used to store each mac id from file

in while() , reads the file till the end of the file and compares with the item to be deleted.If the current item is not match then it is written to new file opened by the stream tmp_fp.finally the _tmp file is renamed to original file and original is removed.

after deleting mac id brr[8],the file becomes

Screenshot from 2016-12-25 22:44:53.png

get the complete code from git

Advertisements