c - 我怎样才能成功地在文件 IO 中使用删除功能 -- C

标签 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

最佳答案

一个问题是您没有比较字符串,而是比较指针。

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)

Note:: strcmp returns 0 if both the strings are same.

For more info 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++;
        }
    }  
} 

if 中的 total--i++ 是不必要的。

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

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

关于c - 我怎样才能成功地在文件 IO 中使用删除功能 -- C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53495916/

相关文章:

c - 通过 C 中的文件使用 crypt 函数进行身份验证(段错误)

Matlab对象实例更新

c - C中的结构误解

c - 向结构体传递 null

c - 为什么这段代码中子进程不会执行?

c - 如何分配内存并将其返回(通过指针参数)给调用函数?

c - 使用 C 语言的文本文件登录系统

arrays - 要从列表中添加或删除的 Golang 映射或结构

arrays - 如何将单个元素添加到数组对象

c - 我想将 stdin 解释为二进制文件。为什么 freopen 在 Windows 上失败?