在此收入管理中我的edit_record功能不起作用

ycggw6v2  于 2023-04-29  发布在  其他
关注(0)|答案(1)|浏览(85)

当我试图编辑记录这个程序写一个新的记录到文件,而不是编辑现有的记录。这段代码除了编辑现有记录外工作正常。

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

struct taxpayer
{
    char name[50];
    int Pancard_Number;
    int Mobileno;
    float income;
    float tax;
};

void add_record(FILE *file)
{
    struct taxpayer record;
    printf("Enter name: ");
    scanf("%s", record.name);
    printf("Enter 5-digit Pancard_number: ");
    scanf("%d", &record.Pancard_Number);
    printf("Enter Mobileno: ");
    scanf("%d", &record.Mobileno);
    printf("Enter income: ");
    scanf("%f", &record.income);

    if (record.income <= 25000)
    {
        record.tax = 0;
    }
    else if (record.income > 25000 && record.income <= 50000)
    {
        record.tax = (record.income - 25000) * 0.05;
    }
    else if (record.income > 50000 && record.income <= 75000)
    {
        record.tax = 1250 + ((record.income - 50000) * 0.2);
    }
    else if (record.income > 75000 && record.income <= 100000)
    {
        record.tax = 5520 + ((record.income - 75000) * 0.3);
    }
    else
    {
        record.tax = 11250 + ((record.income - 100000) * 0.4);
    }

    // record.tax = record.income * 0.1; // 10% tax rate
    fwrite(&record, sizeof(struct taxpayer), 1, file);
    printf("Record added successfully.\n");
}

void show_allrecord(FILE *file)
{
    // rewind(file);
    struct taxpayer record;
    fseek(file, 0, SEEK_SET);
    printf("%-20s %-20s %-20s %-20s  \n", "Name", "PancardNo.", "Income", "TaxPaid");
    while (fread(&record, sizeof(struct taxpayer), 1, file))
    {
        printf("%-20s %-19d Rs.%-19.2f Rs.%-17.2f  \n", record.name, record.Pancard_Number, record.income, record.tax);
    }
}

void search_record(FILE *file)
{
    // char name[50];
    int search;
    struct taxpayer record;
    printf("Enter pancard number to search: ");
    scanf("%d", &search);
    fseek(file, 0, SEEK_SET);
    while (fread(&record, sizeof(struct taxpayer), 1, file))
    {
        if (record.Pancard_Number == search)
        {
            printf("%-20s %-20s %-20s %-20s  \n", "Name", "PancardNo.", "Income", "TaxPaid");

            printf("%-20s %-19d Rs.%-19.2f Rs.%-17.2f  \n", record.name, record.Pancard_Number, record.income, record.tax);

            return;
        }
    }
    printf("Record not found.\n");
}

// void edit_record(FILE *file)
// {
//     char name[50];
//     struct taxpayer record;
//     printf("Enter name to edit: ");
//     scanf("%s", name);
//     fseek(file, 0, SEEK_SET);
//     while (fread(&record, sizeof(struct taxpayer), 1, file))
//     {
//         if (strcmp(record.name, name) == 0)
//         {
//             printf("Enter new income: ");
//             scanf("%f", &record.income);

//             if (record.income <= 25000)
//             {
//                 record.tax = 0;
//             }
//             else if (record.income > 25000 && record.income <= 50000)
//             {
//                 record.tax = (record.income - 25000) * 0.05;
//             }
//             else if (record.income > 50000 && record.income <= 75000)
//             {
//                 record.tax = 1250 + ((record.income - 50000) * 0.2);
//             }
//             else if (record.income > 75000 && record.income <= 100000)
//             {
//                 record.tax = 5520 + ((record.income - 75000) * 0.3);
//             }
//             else
//             {
//                 record.tax = 11250 + ((record.income - 100000) * 0.4);
//             }

//             // record.tax = record.income * 0.1;
//             fseek(file, -sizeof(struct taxpayer), SEEK_CUR);
//             fwrite(&record, sizeof(struct taxpayer), 1, file);
//             printf("Record updated successfully.\n");
//             return;
//         }
//     }
//     printf("Record not found.\n");
// }




FILE *delete_record(FILE *file) {
    char name[30];
    printf("Enter the name of the taxpayer to delete: ");
    scanf("%s", name);
    fseek(file, 0, SEEK_SET);

    struct taxpayer record;
    int found = 0;

    // Create a temporary file
    FILE *temp = fopen("temp.txt", "wb");

    // Read each record from the file and write to the temporary file except for the one to be deleted
    while (fread(&record, sizeof(record), 1, file) == 1) {
        if (strcmp(record.name, name) == 0) {
            found = 1;
        }
        else {
            fwrite(&record, sizeof(record), 1, temp);
        }
    }
    fclose(file);
    fclose(temp);

    if (found) {
        // Delete the original file and rename the temporary file to the original file name
        remove("taxpayers.txt");
        rename("temp.txt", "taxpayers.txt");
        printf("Taxpayer with name %s has been deleted.\n", name);
    } else {
        printf("Taxpayer with name %s not found.\n", name);
        remove("temp.txt");
    }

    // Open the original file for appending
    file = fopen("taxpayers.txt", "ab+");
    if (file == NULL) {
        printf("Error opening file.\n");
        exit(1);
    }
    return file;
}

int main()
{
    FILE *file = fopen("taxpayers.txt", "ab+");
    if (file == NULL)
    {
        printf("Error opening file.\n");
        exit(1);
    }

    int choice;
    do
    {
        printf("\n1. Add New Record\n");
        printf("2. List All Tax Payer along with Income Tax to be paid\n");
        printf("3. Search\n");
        printf("4. Edit\n");
        printf("5. Delete Record\n");
        printf("6. Exit\n");
        printf("Enter your choice: ");
        fflush(stdin);
        scanf("%d", &choice);

        switch (choice)
        {
        case 1:
            add_record(file);
            break;
        case 2:
            show_allrecord(file);
            break;
        case 3:
            search_record(file);
            break;
        case 4:
            edit_record(file);
            break;
        case 5:
            delete_record(file);
            break;
        case 6:
            printf("Exiting...\n");
            break;
        default:
            printf("Invalid choice. Please try again.\n");
            break;
        }
    } while (choice != 6);
    fclose(file);
    return 0;
}

我想要的是它从文件更新现有记录。它还要求用户输入姓名谁的收入你要更新。我希望它能根据pancard号和更新记录来请求pancard号和匹配记录。

7gcisfzg

7gcisfzg1#

The Microsoft fopen documentation说:
当使用"a"访问类型或"a+"访问类型打开文件时,所有写操作都发生在文件末尾。文件指针可以通过使用fseek或rewind重新定位,但在执行任何写入操作之前总是移回文件的末尾。因此,不能覆盖已有数据
[强调我的]
这就解释了你的行为:如果要就地修改数据,则不能使用"a""a+"规范。
要做到这一点,您需要"r+"规范。如果文件不存在,则会失败,因此需要使用"w+"重试。
以后记住"a"的意思是append,i。即添加到末尾。

相关问题