Click here to Skip to main content
15,888,320 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have the following structure and i am writing it to file.
Here are my code to write,display and delete the record from the file.
When i write the record and display it work fine but when i asked to delete the record and again print it id changes.

Here is the output
Enter the choice
   1- Insert a new record into file
   2- Update the record
   3- Display the records
   4- Delete the record
   5- Close the connection with the server
   1
   Enter the id of the record
   1
   Enter name correspond to that id
   ashishi
   Client-write() is OK
   String successfully sent!
   Waiting the 10.0.2.15 to echo back...
   Client-read() is OK
   Echoed data from the following server: CREATED_RECORD
   count incremented
   Enter the choice
   1- Insert a new record into file
   2- Update the record
   3- Display the records
   4- Delete the record
   5- Close the connection with the server
   1
   Enter the id of the record
   2
   Enter name correspond to that id
   tarun
   Client-write() is OK
   String successfully sent!
   Waiting the 10.0.2.15 to echo back...
   Client-read() is OK
   Echoed data from the following server: CREATED_RECORD
   count incremented
   Enter the choice
   1- Insert a new record into file
   2- Update the record
   3- Display the records
   4- Delete the record
   5- Close the connection with the server
   3
   IN DISPLAY
   value of count is 2
   Client-write() is OK
   String successfully sent!
   value of count is 2

   id is  = 1 name is = ashishi
   value of ii is 2
   value of count is 2

   id is  = 2 name is = tarun

   display loop ended
   Enter the choice
   1- Insert a new record into file
   2- Update the record
   3- Display the records
   4- Delete the record
   5- Close the connection with the server
   4
   Enter id to be deleted
   1
   enter to be deleted is 1
   sending
   sent
   Client-write() is OK
   String successfully sent!
   Waiting the 10.0.2.15 to echo back...
   Message from server is DELETED RECORD
   Enter the choice
   1- Insert a new record into file
   2- Update the record
   3- Display the records
   4- Delete the record
   5- Close the connection with the server
   3
   IN DISPLAY
   value of count is 1
   Client-write() is OK
   String successfully sent!
   value of count is 1

   id is  = 1 name is = tarun// why the id changes problem

   display loop ended
   Enter the choice
   1- Insert a new record into file
   2- Update the record
   3- Display the records
   4- Delete the record
   5- Close the connection with the server


And here the code:
C++
struct emprec

{

    int empid;
    int operation;
    char name[20];

};
if (temp2.operation==1)//write record
        {
                count++;
                fwrite(&temp2.empid,sizeof(temp2.empid),1,fd);
                fwrite(&temp2.name,20,1,fd);
                fclose(fd);
}

if (temp2.operation==4)//delete record


            {

                printf("In case 4 THAT IS DELETE \n");
                int found =0,i=1;
                fd = fopen(file_path,"r+");
                if(fd==NULL)
                {
                    printf("File cannot be opened\n");
                    memset(sbuf,0,sizeof(sbuf));
                    strcpy(sbuf,FILE_ERR);
                    printf("sbuf is %s\n",sbuf);
                    rc = write(sd2, sbuf, sizeof(sbuf));
                    return 1;
                }
                fd1 = fopen(file_path1,"a+");
                if(fd1==NULL)
                {
                    printf("File cannot be opened\n");
                    memset(sbuf,0,sizeof(sbuf));
                    strcpy(sbuf,FILE_ERR);
                    printf("sbuf is %s\n",sbuf);
                    rc = write(sd2, sbuf, sizeof(sbuf));
                    perror("fopen");
                    return 1;
                }

                printf("name to search is %s\n",temp2.name);
                printf("id to delete is %d\n",temp2.empid);
                emp temp7,temp10;
                memcpy(&temp10,buffer,sizeof(temp10));
                printf("temp 10 id to delete is %d\n",temp10.empid);
                while(i<=count){
                    fread(&temp7.empid,sizeof(temp7.empid),1,fd);
                    fread(&temp7.name,sizeof(temp7.name),1,fd);

                    if(temp2.empid==temp7.empid){
                        printf("A requested by name found and deleted\n");
                        found = 1;
                    //fread(&temp7.empid,sizeof(temp7.empid),1,fd);
                    //fread(&temp7.name,sizeof(temp7.name),1,fd);
                        count--;
                    }else{
                        fwrite(&temp7,sizeof(temp7),2,fd1);
                        //fwrite(&temp7.empid,sizeof(temp7.empid),1,fd);
                        //fwrite(&temp7.name,20,1,fd);
                        //printf("NEW record in new file we are writing\n");
                        //printf("EMP id written is %d\n",temp7.empid);
                        //printf("EMP name written is %s\n",temp7.name);
                        }
                    if(!found)
                    {
                        printf("cannot found the record\n");
                    }
                    i++;
                }
                fclose(fd);
                fclose(fd1);
                remove(file_path);
                rename(file_path1,file_path);
}
if (temp2.operation==3)//display
                {

                    printf("In display\n");
                    emp temp6;
                    int i =1;
                    fd = fopen(file_path,"r+");
                    if(fd==NULL)
                    {
                        printf("File cannot be opened\n");
                        perror ("fopen");
                        return 1;
                    }

                    while(i<=count)
                    {
                        fread(&temp6.empid,sizeof(temp6.empid),1,fd);
                        fread(&temp6.name,sizeof(temp6.name),1,fd);

                        printf("\nid is  = %d name is = %s\n",temp6.empid,temp6.name);

                        memset(sdata,0,sizeof(temp6));
                        memcpy(sdata,&temp6,sizeof(temp6));
                        printf("temp2.empname is %s\n",temp2.name);
                        rc = write(sd2, sdata, sizeof(temp6));//add a check here
                        sleep(1);
                        i++;
                    }


                    fclose(fd);
Posted
Updated 24-Jul-15 1:16am
v4
Comments
Mohibur Rashid 23-Jul-15 3:39am    
Have you tried debugging?
Richard MacCutchan 23-Jul-15 4:09am    
I cannot see where you actually delete the record, although the line fwrite(&temp7,sizeof(temp7),2,fd1); would seem to be incorrect.

1 solution

First of all, I'm sorry, but your code is really "ugly"!
The variables tempx could have a describing name so it will be easier to follow the flow.
Doesn't make much sense to define a structure than copy only some of its fields in the file.
Instead of:
C++
struct emprec
{
    int empid;
    int operation;
    char name[20];
};

if (temp2.operation==1)    //write record
{
     count++;
     fwrite(&temp2.empid,sizeof(temp2.empid),1,fd);
     fwrite(&temp2.name,20,1,fd);
     fclose(fd);
}

Define a record structure and perform I/O on the whole structure, that is faster and less error prone:
C++
struct Record
{
    int empid;
    char name[20];
};

struct emprec
{
    int operation;
    struct Record record;
};
if (temp2.operation==1)    //write record
{
     count++;
     fwrite(&temp2.record,sizeof(struct Record),1,fd);
     fclose(fd);
}

In this way you would have avoided the error you made in the deleting routine:
C++
 ...
 while(i<=count)
 {
fread(&temp7.empid,sizeof(temp7.empid),1,fd);    //here correctly reads the two
fread(&temp7.name,sizeof(temp7.name),1,fd);      //fields

if(temp2.empid==temp7.empid)
    {
    printf("A requested by name found and deleted\n");
    found = 1;
    count--;
}
    else
    {
    fwrite(&temp7,sizeof(temp7),2,fd1);    //But here you write the whole struct and 2 times its size!
    }
...

Could have been:
C++
 ...
 while(i&lt;=count)
 {
      fread(&temp7.record,sizeof(struct Record),1,fd);

    if(temp2.record.empid==temp7.record.empid)
    {
        printf("A requested by name found and deleted\n");
        found = 1;
        count--;
    }
    else
    {
        fwrite(&temp7.record,sizeof(struct Record),1,fd1);
    }
...
 
Share this answer
 
v6

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900