C++ 从文件(随机访问文件,C+;+;)写入和读取数据时丢失数据

C++ 从文件(随机访问文件,C+;+;)写入和读取数据时丢失数据,c++,file,fstream,random-access,randomaccessfile,C++,File,Fstream,Random Access,Randomaccessfile,首先,我通过写入以下数据初始化“hardware.dat”文件,然后以列表形式显示它们。 但是,当我处理选项2,然后在不编辑或添加任何内容的情况下退出选项2时,所有数据都会丢失。我建议任何事情都不会改变。 为什么会存在这个问题?如何修复它 谢谢你的关注 代码: int question_3() { cout << "Question 3" << endl; create_One_Hundred_blank_Data(); char choic

首先,我通过写入以下数据初始化“hardware.dat”文件,然后以列表形式显示它们。

但是,当我处理选项2,然后在不编辑或添加任何内容的情况下退出选项2时,所有数据都会丢失。我建议任何事情都不会改变。

为什么会存在这个问题?如何修复它

谢谢你的关注


代码:

int question_3()
{
    cout << "Question 3" << endl;
    create_One_Hundred_blank_Data();
    char choice = '0';
    do
    {
    cout << "---------------Main Menu---------------" << endl
         << "1. Initialize hardware data." << endl
         << "2. Add / Edit data." << endl
         << "3. Remove data." << endl
         << "4. Show whole data in the file." << endl;
    cout << "Enter choice > ";      choice = getch();       cout << choice << endl;     // #include <conio.h>
    switch_Choice(choice);
    }while (choice != '0');

    cout << "Program is ended." << endl;
    return 0;
}


void switch_Choice(char choice)
{
    switch (choice)
    {
        case '1':
            choice_1();
            break;

        case '2':
            choice_2();
            break;

        case '3':
            choice_3();
            break;

        case '4':
            choice_4();
            break;
    }
}



void choice_2()
{
    hardware.open("hardware.dat", ios::binary | ios::out);
    if (!hardware)
    {
        cerr << "File could not be opened." << endl;
        exit(1);
    }

    int record = 0;
    string tool_name = "";
    int quantity = 0;
    int cost = 0;
    string buffer_Eater = "";

        cout << "Enter record number <O to exit input> : ";     cin >> record;
    while(record != 0)
    {
        cout << "Enter tool name                       : ";     getline(cin, tool_name); getline(cin, buffer_Eater);
        cout << "Enter quantity                        : ";     cin >> quantity;
        cout << "Enter cost                            : ";     cin >> cost; 

        write_Single_Data(myHardwareData, record, tool_name, quantity, cost);

        cout << "Enter record number <O to exit input> : ";     cin >> record;
    }
    hardware.close();
    output_Whole_File_Data();
    separation_line();
}



void output_Whole_File_Data()
{
    hardware.open("hardware.dat", ios::binary | ios::in);
    output_Data_LineHead();
    hardware.read(reinterpret_cast<char *>(&myHardwareData), sizeof(HardwareData));
    int counter = 0;
    cout << setprecision(2) << fixed;
    while (hardware && !hardware.eof())
    {
        if (myHardwareData.getRecord() != 0)
            output_Data_Line(cout, myHardwareData);

        hardware.read(reinterpret_cast<char *>(&myHardwareData), sizeof(HardwareData));
    }
    hardware.close();
}
int问题_3()
{
不能尝试替换

hardware.open("hardware.dat", ios::binary | ios::out);


。但是,如果我想使用choice_2更改记录3的工具名称,它会在文件末尾追加一行新的记录数据。此外,需要在文件中保留100条记录。有些记录为0,因此不会显示。如果使用ios::app,则会有100多条记录。Thx。@CasperLi如果不为ap打开pend,文件被截断。iso::out擦除每个硬件中的所有数据。open(…iso::out)。任何其他替代方法都可以替换iso::out?@CasperLi替代方法是将“| ios::app”附加到您的
open
调用中。
hardware.open("hardware.dat", ios::binary | ios::out | ios::app);