Reading and writing binary file

2019-01-01 10:30发布

问题:

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 );

回答1:

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<unsigned char> buffer(std::istreambuf_iterator<char>(input), {});

}


回答2:

 sizeof(buffer) == sizeof(char*) 

Use length instead.

Also, better to use fopen with \"wb\"....



回答3:

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;
}


回答4:

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



回答5:

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



回答6:

There is a much simpler way. This does not care if it is binary or text file.

Use noskipws.

char buf[SZ];
ifstream f(\"file\");
int i;
for(i=0; f >> noskipws >> buffer[i]; i++);
ofstream f2(\"writeto\");
for(int j=0; j < i; j++) f2 << noskipws << buffer[j];


回答7:

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<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.