根本原因
  
  
   根本原因或问题是打开文件的模式。我的实验表明,只有在使用
   
    std::ios_base::app
   
   .  然而,大多数文档都暗示
   
    全部的
   
   写入操作将附加到文件。寻找一个位置,然后写入仍将在EOF写入数据。
  
  
   为了在文件开头写入,而不进行截断
   
    ofstream
   
   必须用打开
   
    std::ios_base::in
   
   和
   
    std::ios_base::out
   
   属性。
  
  
   已更正的程序
  
  
   我修改了我的程序,使记录在16字节边界上排列,未使用的字节用0xFF填充(这使十六进制转储更容易读取)。所有整数数据均为32位;文本长度可变。
  
  
   
    首先写入记录数据,以附加到文件中。使用两个不同的变量打开文件两次,每种模式打开一次。
   
  
  #include <fstream>
#include <string>
struct Table_Quantity_Record
{
    unsigned int    quantity;
    uint8_t         padding[12];
};
struct Record
{
    unsigned int    id;
    std::string     text;
};
int main()
{
    static const Record     table[] =
    {
        { 0x11111111, "Apple"},
        { 0x22222222, "Salt"},
        { 0x33333333, "Butter"},
        { 0x44444444, "Carrot"},
        { 0x55555555, "Plum"},
    };
    static const size_t records_in_table =
        sizeof(table) / sizeof(table[0]);
    static const char   table_filename[] = "record_file.bin";
    std::remove(&table_filename[0]);
    size_t  i;
    Table_Quantity_Record   quantity_record;
    quantity_record.quantity = 1;
    std::fill(&quantity_record.padding[0],
              &quantity_record.padding[12],
              0xffu);
    static const uint8_t    padding_bytes[16] = {0xFFu};
    for (i = 0; i < records_in_table; ++i)
    {
        // Open the file in append mode, and append the new data record.
        std::ofstream   data_file(&table_filename[0],
                                  std::ios_base::binary | std::ios_base::app | std::ios_base::ate);
        if (data_file)
        {
            data_file.write((char *) &table[i].id, sizeof(Record::id));
            const unsigned int length = table[i].text.length();
            data_file.write((char *) &length, sizeof(length));
            data_file.write(table[i].text.c_str(), length);
            data_file.flush();
            const unsigned int padding_qty =
                16 - sizeof(Record::id) - sizeof(length) - length;
            static const uint8_t pad_byte = 0xFFU;
            for (size_t j = 0; j < padding_qty; ++j)
            {
                data_file.write((char *) &pad_byte, sizeof(pad_byte));
            }
            data_file.flush();
            data_file.close();
        }
        // Open the data file with "in" attribute to write the record quantity
        // at the beginning of the file.
        std::ofstream   table_file(&table_filename[0],
                                   std::ios_base::binary | std::ios_base::in);
        table_file.write((char *) &quantity_record, sizeof(quantity_record));
        table_file.flush();
        table_file.close();
        ++quantity_record.quantity;
    }
    return 0;
}
  
   二进制文件的内容
  
  $ od -Ax -x record_file.bin
000000 0005 0000 ffff ffff ffff ffff ffff ffff
000010 2222 2222 0004 0000 6153 746c ffff ffff
000020 3333 3333 0006 0000 7542 7474 7265 ffff
000030 4444 4444 0006 0000 6143 7272 746f ffff
000040 5555 5555 0004 0000 6c50 6d75 ffff ffff
000050
  
   
     
   
  
  
   
    注意:自问题中的程序以来,记录ID值已更改,以便于查找记录。