从文件中删除记录,打印时出错 [英] Deleting a record from file, error while printing

查看:74
本文介绍了从文件中删除记录,打印时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下结构,我将其写入文件。

这是我的代码,用于编写,显示和删除文件中的记录。

当我写下记录并显示它工作正常但当我要求删除记录并再次打印它ID更改。



这是输出

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:

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);

推荐答案

首先,我很抱歉,但你的代码真的很难看!

变量 tempx 可能有一个描述名称,因此更容易遵循流程。

定义结构比仅复制更有意义文件中的一些字段。

而不是:

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:
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);
}



定义记录结构并在整个结构上执行I / O,这样更快,更不容易出错:


Define a record structure and perform I/O on the whole structure, that is faster and less error prone:

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:

 ...
 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:

 ...
 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);
    }
...


这篇关于从文件中删除记录,打印时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆