C 中的删除函数出现问题:“找不到数据”错误

问题描述 投票:0回答:1

C语言代码

*我的半代码无法正常运行,特别是删除函数,该函数应该从文件中删除信息,但它显示“找不到数据”。 我尝试询问一些AI,但他们无法帮助我,总的来说它也给了我找不到数据,但它必须从我电脑上的txt文件中删除信息

#include <stdio.h>
#include <wchar.h>

struct DataXY {
    int cod;
    wchar_t Name[20], surname[20], bat[20];
    int year;
};

void search() {
    int SCod;
    FILE *file;
    printf("Enter cod: ");
    scanf("%d", &SCod);

    file = _wfopen(L"C:\\Users\\burma\\Desktop\\FILES.txt", L"r, ccs=UTF-8");
    if (file == NULL) {
        printf("Error: Unable to open the file.\n");
        return;
    }

    struct DataXY fulik;
    int found = 0;
    while (fwscanf(file, L"%d %ls %ls %ls %d", &fulik.cod, fulik.Name, fulik.surname, fulik.bat, &fulik.year) == 5) {
        if (SCod == fulik.cod) {
            found = 1;
            printf("Cod: %d\n", fulik.cod);
            wprintf(L"Name: %ls\n", fulik.Name);
            wprintf(L"Surname: %ls\n", fulik.surname);
            wprintf(L"Bat: %ls\n", fulik.bat);
            printf("Year: %d\n", fulik.year);
            printf("-------------\n");
        }
    }

    fclose(file);

    if (!found) {
        printf("Data not found\n");
    }
}
void Dele() {
    struct DataXY fulik;
    FILE *file;
    FILE *tempFile = fopen("C:\\Users\\burma\\Desktop\\temp.txt", "w");
    if (tempFile == NULL) {
        printf("Error: Unable to create temporary file.\n");
        return;
    }
    int delCod;
    printf("Enter cod to delete: ");
    scanf("%d", &delCod);

    file = fopen("C:\\Users\\burma\\Desktop\\FILES.txt", "r+");
    if (file == NULL) {
        printf("Error: Unable to open the file.\n");
        fclose(tempFile);
        return;
    }
    int found1 = 0;
    rewind(file);

    while (fscanf(file, "%d %ls %ls %ls %d", &fulik.cod, fulik.Name, fulik.surname, fulik.bat, &fulik.year) == 5) {
        if (delCod != fulik.cod) {
            fwprintf(tempFile, L"%d %ls %ls %ls %d\n", fulik.cod, fulik.Name, fulik.surname, fulik.bat, fulik.year);
        } else {
            found1 = 1;
        }
    }

    fclose(file);
    fclose(tempFile);

    if (found1==0) {
        printf("Data not found\n");
        remove("C:\\Users\\burma\\Desktop\\temp.txt");
        return;
    }

    remove("C:\\Users\\burma\\Desktop\\FILES.txt");
    rename("C:\\Users\\burma\\Desktop\\temp.txt", "C:\\Users\\burma\\Desktop\\FILES.txt");
}


int main() {
    int Menu;
    struct DataXY fulik;
    FILE *file;
    do {
        printf("1. Add data\n");
        printf("2. Show data\n");
        printf("3. Search\n");
        printf("4. Edit data\n");
        printf("5. Delete data\n");
        printf("6. Exit\n");
        scanf("%d", &Menu);

        switch (Menu) {
            case 1:
                printf("Enter cod: ");
                scanf("%d", &fulik.cod);
                wprintf(L"Enter Name: ");
                wscanf(L"%ls", fulik.Name);
                wprintf(L"Enter Surname: ");
                wscanf(L"%ls", fulik.surname);
                wprintf(L"Enter Bat: ");
                wscanf(L"%ls", fulik.bat);
                printf("Enter Year: ");
                scanf("%d", &fulik.year);

                file = _wfopen(L"C:\\Users\\burma\\Desktop\\FILES.txt", L"a, ccs=UTF-8");
                if (file == NULL) {
                    printf("Error: Unable to open the file.\n");
                    return 1;
                }
                fwprintf(file, L"%d %ls %ls %ls %d\n", fulik.cod, fulik.Name, fulik.surname, fulik.bat, fulik.year);
                fclose(file);
                break;

            case 2:
                file = _wfopen(L"C:\\Users\\burma\\Desktop\\FILES.txt", L"r, ccs=UTF-8");
                if (file == NULL) {
                    printf("Error: Unable to open the file.\n");
                    return 1;
                }

                printf("Data in the file:\n");
                while (fwscanf(file, L"%d %ls %ls %ls %d", &fulik.cod, fulik.Name, fulik.surname, fulik.bat, &fulik.year) == 5) {
                    printf("Cod: %d\n", fulik.cod);
                    wprintf(L"Name: %ls\n", fulik.Name);
                    wprintf(L"Surname: %ls\n", fulik.surname);
                    wprintf(L"Bat: %ls\n", fulik.bat);
                    printf("Year: %d\n", fulik.year);
                    printf("-------------\n");
                }
                fclose(file);
                break;

            case 3:
                search(); 
                break;

            default:
                if (Menu != 6) {
                    printf("Invalid option!\n");
                }
                break;
    
            case 4:
                Dele();
                break;
        }
    } while (Menu != 6);

    return 0;
}
c function
1个回答
0
投票
  1. 您的菜单显示“5.删除数据”,但您的代码使用

    case 4:

  2. 在 Linux 上,似乎您必须将标准输出设置为宽模式才能使

    fprintf()
    wscanf()
    工作,然后常规
    scanf()
    printf()
    功能停止工作。
    wfscanf()
    在Linux上似乎不存在,所以我使用了
    fscanf()

#include <stdio.h>
#include <wchar.h>

struct DataXY {
    int cod;
    wchar_t Name[20], surname[20], bat[20];
    int year;
};

void search() {
    int SCod;
    FILE *file;
    wprintf(L"Enter cod: ");
    wscanf(L"%d", &SCod);

    file = fopen("FILES.txt", "r, ccs=UTF-8");
    if (file == NULL) {
        wprintf(L"Error: Unable to open the file.\n");
        return;
    }

    struct DataXY fulik;
    int found = 0;
    while (fwscanf(file, L"%d %ls %ls %ls %d", &fulik.cod, fulik.Name, fulik.surname, fulik.bat, &fulik.year) == 5) {
        if (SCod == fulik.cod) {
            found = 1;
            wprintf(L"Cod: %d\n", fulik.cod);
            wprintf(L"Name: %ls\n", fulik.Name);
            wprintf(L"Surname: %ls\n", fulik.surname);
            wprintf(L"Bat: %ls\n", fulik.bat);
            wprintf(L"Year: %d\n", fulik.year);
            wprintf(L"-------------\n");
        }
    }

    fclose(file);

    if (!found) {
        wprintf(L"Data not found\n");
    }
}
void Dele() {
    struct DataXY fulik;
    FILE *file;
    FILE *tempFile = fopen("temp.txt", "w, ccs=UTF-8");
    if (tempFile == NULL) {
        wprintf(L"Error: Unable to create temporary file.\n");
        return;
    }
    int delCod;
    wprintf(L"Enter cod to delete: ");
    wscanf(L"%d", &delCod);

    file = fopen("FILES.txt", "r+, ccs=UTF-8");
    if (file == NULL) {
        wprintf(L"Error: Unable to open the file.\n");
        fclose(tempFile);
        return;
    }
    int found1 = 0;
    rewind(file);

    while (fscanf(file, "%d %ls %ls %ls %d", &fulik.cod, fulik.Name, fulik.surname, fulik.bat, &fulik.year) == 5) {
        if (delCod != fulik.cod) {
            fwprintf(tempFile, L"%d %ls %ls %ls %d\n", fulik.cod, fulik.Name, fulik.surname, fulik.bat, fulik.year);
        } else {
            found1 = 1;
        }
    }

    fclose(file);
    fclose(tempFile);

    if (found1==0) {
        wprintf(L"Data not found\n");
        remove("temp.txt");
        return;
    }

    remove("FILES.txt");
    rename("temp.txt", "FILES.txt");
}


int main() {
#if __linux__
    fwide(stdout, 1);
#endif
    int Menu;
    struct DataXY fulik;
    FILE *file;
    do {
        wprintf(L"1. Add data\n");
        wprintf(L"2. Show data\n");
        wprintf(L"3. Search\n");
        wprintf(L"4. Edit data\n");
        wprintf(L"5. Delete data\n");
        wprintf(L"6. Exit\n");
        wscanf(L"%d", &Menu);

        switch (Menu) {
            case 1:
                wprintf(L"Enter cod: ");
                wscanf(L"%d", &fulik.cod);
                wprintf(L"Enter Name: ");
                wscanf(L" %ls", fulik.Name);
                wprintf(L"Enter Surname: ");
                wscanf(L"%ls", fulik.surname);
                wprintf(L"Enter Bat: ");
                wscanf(L"%ls", fulik.bat);
                wprintf(L"Enter Year: ");
                wscanf(L"%d", &fulik.year);

                file = fopen("FILES.txt", "a, ccs=UTF-8");
                if (file == NULL) {
                    wprintf(L"Error: Unable to open the file.\n");
                    return 1;
                }
                fwprintf(file, L"%d %ls %ls %ls %d\n", fulik.cod, fulik.Name, fulik.surname, fulik.bat, fulik.year);
                fclose(file);
                break;
            case 2:
                file = fopen("FILES.txt", "r, ccs=UTF-8");
                if (file == NULL) {
                    wprintf(L"Error: Unable to open the file.\n");
                    return 1;
                }

                wprintf(L"Data in the file:\n");
                while (fscanf(file, "%d %ls %ls %ls %d", &fulik.cod, fulik.Name, fulik.surname, fulik.bat, &fulik.year) == 5) {
                    wprintf(L"Cod: %d\n", fulik.cod);
                    wprintf(L"Name: %ls\n", fulik.Name);
                    wprintf(L"Surname: %ls\n", fulik.surname);
                    wprintf(L"Bat: %ls\n", fulik.bat);
                    wprintf(L"Year: %d\n", fulik.year);
                    wprintf(L"-------------\n");
                }
                fclose(file);
                break;

            case 3:
                search();
                break;
            case 5:
                Dele();
                break;
            default:
                if (Menu != 6)
                    wprintf(L"Invalid option!\n");
                break;
        }
    } while (Menu != 6);

    return 0;
}

和示例会话:

1. Add data                                                                                                            
2. Show data                                                                                                           
3. Search                                                                                                              
4. Edit data                                                                                                           
5. Delete data
6. Exit
1
Enter cod: 1
Enter Name: a
Enter Surname: b
Enter Bat: c
Enter Year: 2
1. Add data
2. Show data
3. Search
4. Edit data
5. Delete data
6. Exit
5
Enter cod to delete: 1
1. Add data
2. Show data
3. Search
4. Edit data
5. Delete data
6. Exit
2
Data in the file:
1. Add data
2. Show data
3. Search
4. Edit data
5. Delete data
6. Exit
© www.soinside.com 2019 - 2024. All rights reserved.