如何在文件IO中成功使用删除功能-C

时间:2018-11-27 08:58:03

标签: c arrays struct file-io text-files

我有一个文本文件存在默认记录(3条记录:1001、1002、1003)。现在,我想调用删除函数并使用记录号删除分配记录。但是,存在一些问题... 问题:我无法使用输入的记录号删除分配记录

结果:

Please enter a record number you wanted to delete: 1002

you have entered: 1002

Record in file before delete:
Total record: 3
1001 eric 1 human 10 70.00 home arrived
1002 amy  1 human 20 45.44 home arrived
1003 Tom  3 human 30 10.00 home arrived

DO you want to delete other records? <y/n>: n

删除后文本文件中的内容:

1001 eric 1 human 10 70.00 home arrived
1001 eric 1 human 10 70.00 home arrived
1001 eric 1 human 10 70.00 home arrived

这是我的代码(很抱歉,我的长代码很想解决这个问题):

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(){
    Del_menu();
    return 0;
}

// function1
int Del_menu() {
    while (1) {
        printf("Please enter a record number you wanted to delete : ");
        Del();
        printf("\nDo u want to delete  other record? Enter 'y' for yes and 'n' for no :");
        char ans;
        scanf(" %c", &ans);
        if (ans == 'N' || ans == 'n') {
            break;
        }
    }
}

// function2
int Del() {
    struct record {
        char recordnum[40];
        char itemrecord[40];
        char quantity[40];
        char weight[40];
        char itemname[40];
        char catagory[40];
        char recipient[40];
        char final_destination[40];
        char status[40];
    };

    FILE *fileptr1, *fileptr2;
    char filename[40] = "data.txt";
    int total = 0;  
    int  total_1 = 0 ,total_2 = 0, i = 0 , temp;

    fileptr1 = fopen(filename, "r");
    fscanf(fileptr1, "%d", &total);
    int c = total;
    struct record Arr[c];

    total_1 = total; 

    while (total > 0) {  // put data in array
        fscanf(fileptr1, "%s %s %s %s %s %s %s %s %s",
                         Arr[i].recordnum,
                         Arr[i].itemname,
                         Arr[i].itemrecord,
                         Arr[i].catagory, 
                         Arr[i].quantity, 
                         Arr[i].weight,
                         Arr[i].recipient,
                         Arr[i].final_destination, 
                         Arr[i].status); 

        i++;
        total--;
    }

    fseek(stdin, 0, SEEK_END); // clean buffer
    char del_data[41];
    fgets(del_data, 40, stdin);
    del_data[strlen(del_data) - 1] = '\0';
    printf("\nyou have entered :%s\n", del_data);

    total = total_1 ;    //let total change in to default data
    total_2 = total - 1; //total_2 = 2

    printf("\nRecord in file before delete:\n");
    printf("Total record: %d\n",total);
    for(i=0; i<total ;i++) { // output data in the array`
        printf("%s %s %s %s %s %s %s %s %s \n",
               Arr[i].recordnum, 
               Arr[i].itemname,
               Arr[i].itemrecord,
               Arr[i].catagory, 
               Arr[i].quantity, 
               Arr[i].weight,
               Arr[i].recipient,
               Arr[i].final_destination, 
               Arr[i].status);
    }

    rewind(fileptr1);
    fseek(stdin, 0, SEEK_END); // clean buffer 

    fileptr2 = fopen("copy.c", "w");

    while(total != 0) {
        for(i = 0; i < total; i++) {
            if (Arr[i].recordnum != del_data) { 
                fprintf(fileptr2, "%s %s %s %s %s %s %s %s %s",
                                  Arr[i].recordnum, 
                                  Arr[i].itemname,
                                  Arr[i].itemrecord,
                                  Arr[i].catagory,
                                  Arr[i].quantity, 
                                  Arr[i].weight,
                                  Arr[i].recipient,
                                  Arr[i].final_destination,
                                  Arr[i].status); 
                total--;
                i++;
            }
        }
    }  

    fclose(fileptr1);
    fclose(fileptr2);

    remove(filename);
    // rename the file copy.c to original name
    rename("copy.c", filename);
} // end of function 2

1 个答案:

答案 0 :(得分:2)

一个问题是您不是在比较字符串而是在比较指针。

char del_data[41];
char recordnum [40];
if (Arr[i].recordnum != del_data) // Here you are comparing the pointers

将其更改为:

if (strcmp(Arr[i].recordnum, del_data) != 0)
  

注意:如果两个字符串相同,则strcmp返回0

     

有关更多信息,strcmp man page


另一个问题是,您不需要两个循环就可以将数组内容复制到新文件中。

while (total != 0) {
    for(i=0; i < total; i++) {
        if (Arr[i].recordnum != del_data) {
            fprintf(fileptr2, "%s %s %s %s %s %s %s %s %s", 
                              Arr[i].recordnum, 
                              Arr[i].itemname,
                              Arr[i].itemrecord,
                              Arr[i].catagory,
                              Arr[i].quantity,
                              Arr[i].weight,
                              Arr[i].recipient,
                              Arr[i].final_destination, 
                              Arr[i].status); 
            total--;
            i++;
        }
    }  
} 

total--中不需要i++if的地方。

您可以简单地将其重写为:

for(i = 0; i < total; i++) {
    if (strcmp(Arr[i].recordnum, del_data) != 0) {
        fprintf(fileptr2, "%s %s %s %s %s %s %s %s %s",
                          Arr[i].recordnum, 
                          Arr[i].itemname,
                          Arr[i].itemrecord,
                          Arr[i].catagory, 
                          Arr[i].quantity, 
                          Arr[i].weight,
                          Arr[i].recipient,
                          Arr[i].final_destination, 
                          Arr[i].status);
    } 
}