Can't Over write on a specific location on a file. Over write on a specific location deletes all the contents before it and moves the value after it

爷,独闯天下 提交于 2019-12-24 09:55:16

问题


In my project, there is a need to read and write to binary file, basically serializing linked lists in a file, where I store the value in binary format and remember the tellp()/tellg() offset, how ever, I cannot do that. That erases all the contents in it to zero and instead of inserting it pushes the current content back.

For Example, in the below program, I open a file, write values say 1,120,323. Then close it and read it, it shows the exact correct values 1,120,323. But When I try to replace the value of 120->220, 1 becomes zero and the value read as 0 220 220. Basically 220 over writes and pushes 323 back.

#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:


For output streams, opening with std::ios::out is equivalent to std::ios::out | std::ios::trunc, so when you declare std::ofstream file_write for the second time, the previously written content gets discarded and you're writing the file anew. When you then do file_write.seekp(sizeof(int)); on an empty stream, zero bytes are written. If you needed to append to an ofstream then you would open it with std::ios:app. That wouldn't truncate the file, but it would only let you append to it on the other hand.

If you want to do random-access writing on an file stream, you need to declare it as fstream and open it in both reading and writing mode. So what you need is:

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



来源:https://stackoverflow.com/questions/10096196/cant-over-write-on-a-specific-location-on-a-file-over-write-on-a-specific-loca

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!