无法覆盖文件上的特定位置。在特定位置上重写将删除之前的所有内容,并在其后移动值

时间:2012-04-10 20:44:52

标签: c++ fstream random-access

在我的项目中,需要读取和写入二进制文件,基本上序列化文件中的链表,我以二进制格式存储值并记住tellp()/ tellg()偏移,但是,我不能这样做。将其中的所有内容擦除为零,而不是插入它将当前内容推回。

例如,在下面的程序中,我打开一个文件,写入值为1,120,323。然后关闭它并读取它,它显示正确的值1,120,323。但是当我尝试替换120-> 220的值时,1变为零并且值读取为0 220 220.基本上220次写入并推回323。

#include <iostream>
#include <fstream>
#include <cstdlib>

int main() {
  std::cout<<"File Ofstream Testing "<<std::endl;   

  const char * file_name = "Test_File.bin";
  int ONE = 1;
  int ZERO = 0;

  int ONE_TWENTY = 120;
  int TWO_TWENTY = 220;

  int THREE_TWENTY_THREE = 323;
  int THREE_FORTY_FIVE = 345;

//---------------------------------------------------------------------------
{
 std::ofstream file_write(file_name, std::ios::out|std::ios::binary);

 if(!(file_write.is_open())) {
    std::cout<<"File cannot be opened "<<std::endl;
    exit(0);
 }

 file_write.seekp(0);
 file_write.write((char *)&ONE,sizeof(int));
 file_write.write((char *)&ONE_TWENTY,sizeof(int));
 file_write.write((char *)&THREE_TWENTY_THREE,sizeof(int));

 file_write.close();
}
//---------------------------------------------------------------------------
{
 std::ifstream file_read(file_name,std::ios::in|std::ios::binary);

 if(!(file_read.is_open())) {
        std::cout<<"File cannot be opened "<<std::endl;
        exit(0);
 }

 int temp;
 file_read.seekg(0);
 file_read.read((char *)&temp,sizeof(int));
 std::cout<<"Temp "<<temp<<std::endl;
 file_read.read((char *)&temp,sizeof(int));
 std::cout<<"Temp "<<temp<<std::endl;
 file_read.read((char *)&temp,sizeof(int));
 std::cout<<"Temp "<<temp<<std::endl;

 file_read.close();
}
  //---------------------------------------------------------------------------
{
  std::ofstream file_write(file_name, std::ios::out|std::ios::binary);

  if(!(file_write.is_open())) {
   std::cout<<"File cannot be opened "<<std::endl;
   exit(0);
 }

 file_write.seekp(sizeof(int));
 file_write.write((char *)&TWO_TWENTY,sizeof(int));
 file_write.seekp(sizeof(int) + sizeof(int));
 file_write.write((char *)&THREE_FORTY_FIVE,sizeof(int));

 file_write.close();
}
//--------------------------------------------------------------------------
{
  std::ifstream file_read(file_name,std::ios::in|std::ios::binary);

  if(!(file_read.is_open())) {
    std::cout<<"File cannot be opened "<<std::endl;
    exit(0);
}

  int temp;
  file_read.seekg(0);
  file_read.read((char *)&temp,sizeof(int));
  std::cout<<"Temp "<<temp<<std::endl;
  file_read.seekg(sizeof(int));
  file_read.read((char *)&temp,sizeof(int));
  std::cout<<"Temp "<<temp<<std::endl;
  file_read.seekg(sizeof(int) + sizeof(int));
  file_read.read((char *)&temp,sizeof(int));
  std::cout<<"Temp "<<temp<<std::endl;

   file_read.close();
 }
 //--------------------------------------------------------------------------------
 return 0;
}//fn:main

1 个答案:

答案 0 :(得分:1)

对于输出流,使用std::ios::out打开相当于std::ios::out | std::ios::trunc,因此当您第二次声明std::ofstream file_write时,先前写入的内容将被丢弃并且您正在撰写文件重新。然后,当您在空流上执行file_write.seekp(sizeof(int));时,将写入零字节。    如果您需要附加到ofstream,则可以使用std::ios:app打开它。这不会截断文件,但另一方面它只会让你附加到它。

如果要在文件流上进行随机访问写入,则需要将其声明为fstream并在读取和写入模式下打开它。所以你需要的是:

std::fstream file_write(file_name, std::ios::in | std::ios::out | std::ios::binary);