2014-02-10 45 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