2014-02-10 48 views
1

首先,通過寫入以下數據初始化「hardware.dat」文件,然後以列表形式顯示它們。 enter image description here enter image description here在寫入數據和從文件讀取數據時丟失數據(隨機訪問文件,C++)

然而,當我處理選擇2,然後退出選擇2無需編輯或添加任何東西,所有的數據丟失。我建議任何事情都不會改變。 enter image description here

爲什麼存在這個問題?如何解決它?

感謝您的關注。


代碼:

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(); 
} 
+4

[''時總是錯的(EOF!)](HTTP:// stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong)。 – 2014-02-10 12:02:14

回答

1

嘗試更換

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

通過

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

但是,如果我想通過使用choice_2更改記錄3的工具名稱,它會將新行記錄數據附加到文件末尾。而且,需要在文件中保留100條記錄。有些記錄是0,所以不會顯示。如果使用ios :: app,則會有超過100條記錄。謝謝。 – Casper

+0

@CasperLi如果你沒有打開追加,文件會被截斷。 – molbdnilo

+0

iso :: out在每個hardware.open(... iso :: out)中清除所有數據。任何其他的替代品可以取代iso :: out? – Casper