Reading and writing binary file

前端 未结 7 570
挽巷
挽巷 2020-11-22 16:29

I\'m trying to write code to read a binary file into a buffer, then write the buffer to another file. I have the following code, but the buffer only stores a couple of ASCI

7条回答
  •  有刺的猬
    2020-11-22 16:42

    It can be done with simple commands in the following snippet.

    Copies the whole file of any size. No size constraint!

    Just use this. Tested And Working!!

    #include
    #include
    using namespace std;
    int main()
    {
      ifstream infile;
      infile.open("source.pdf",ios::binary|ios::in);
    
      ofstream outfile;
      outfile.open("temppdf.pdf",ios::binary|ios::out);
    
      int buffer[2];
      while(infile.read((char *)&buffer,sizeof(buffer)))
      {
          outfile.write((char *)&buffer,sizeof(buffer));
      }
    
      infile.close();
      outfile.close();
      return 0;
    }
    

    Having a smaller buffer size would be helpful in copying tiny files. Even "char buffer[2]" would do the job.

提交回复
热议问题