Home » C++ » Reading and writing binary file

Reading and writing binary file

Posted by: admin November 29, 2017 Leave a comment

Questions:

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 ASCII characters from the first line in the file and nothing else.

int length;
char * buffer;

ifstream is;
is.open ("C:\Final.gif", ios::binary );
// get length of file:
is.seekg (0, ios::end);
length = is.tellg();
is.seekg (0, ios::beg);
// allocate memory:
buffer = new char [length];
// read data as a block:
is.read (buffer,length);
is.close();

FILE *pFile;
pFile = fopen ("C:\myfile.gif", "w");
fwrite (buffer , 1 , sizeof(buffer) , pFile );
Answers:

If you want to do this the C++ way, do it like this:

#include <fstream>
#include <iterator>
#include <algorithm>

int main()
{
    std::ifstream input( "C:\Final.gif", std::ios::binary );
    std::ofstream output( "C:\myfile.gif", std::ios::binary );

    std::copy( 
        std::istreambuf_iterator<char>(input), 
        std::istreambuf_iterator<char>( ),
        std::ostreambuf_iterator<char>(output));
}

If you need that data in a buffer to modify it or something, do this:

#include <fstream>
#include <iterator>
#include <vector>

int main()
{
    std::ifstream input( "C:\Final.gif", std::ios::binary );
    // copies all data into buffer
    std::vector<char> buffer((
            std::istreambuf_iterator<char>(input)), 
            (std::istreambuf_iterator<char>()));
}

Questions:
Answers:
 sizeof(buffer) == sizeof(char*) 

Use length instead.

Also, better to use fopen with “wb“….

Questions:
Answers:

Here is a short example, the C++ way using rdbuf. I got this from the web. I can’t find my original source on this:

#include <fstream>
#include <iostream>

int main () 
{
  std::ifstream f1 ("C:\me.txt",std::fstream::binary);

  std::ofstream f2 ("C:\me2.doc",std::fstream::trunc|std::fstream::binary);

  f2<<f1.rdbuf();

  return 0;
}

Questions:
Answers:

sizeof(buffer) is the size of a pointer on your last line NOT the actual size of the buffer.
You need to use “length” that you already established instead

Questions:
Answers:

You should pass length into fwrite instead of sizeof(buffer).

Questions:
Answers:

I seriously don’t understand why would someone choose to write such a complicated code when it can happen with such a simple commands in the following snippet.

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

Just use this. Tested And Working!!

#include<iostream>
#include<fstream>
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.