1

Firstly, I initialize the "hardware.dat" file by writing the following data and then display them in list form. enter image description here enter image description here

However, when I process choice 2 and then exit choice 2 without editing or adding anything, all the data lose. I propose that anything will not be changed. enter image description here

Why the problem exist? How to fix it?

Thank you for your attention.


Code :

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();
}
Casper
  • 3,259
  • 9
  • 37
  • 64
  • 4
    [`while (!eof)` is always wrong](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). –  Feb 10 '14 at 12:02

1 Answers1

1

Try to replace

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

by

hardware.open("hardware.dat", ios::binary | ios::out | ios::app);
taktak004
  • 634
  • 7
  • 26
  • However, if I want to change the tool name of record 3 by using choice_2, it will append a new line of record data to the end of file. Moreover, it is required to maintain 100 records in the file. Some of the records are 0 so it will not be displayed. If ios::app is used, there will be more then 100 records. Thx. – Casper Feb 10 '14 at 13:17
  • @CasperLi If you don't open for append, the file gets truncated. – molbdnilo Feb 10 '14 at 13:48
  • iso::out erase all my data in every hardware.open(...iso::out). Any other alternatives can replace iso::out? – Casper Feb 10 '14 at 14:19
  • @CasperLi An alternative is to append "| ios::app" to your `open` call. – Thomas Matthews Feb 10 '14 at 14:52