Odczyt i zapis pliku binarnego

Próbuję napisać kod, aby odczytać plik binarny do bufora, a następnie zapisać go do innego pliku. Mam następujący kod, ale bufor przechowuje tylko kilka znaków ASCII z pierwszej linii w pliku i nic więcej.

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 );
Author: gsamaras, 2011-03-24

7 answers

Jeśli chcesz to zrobić w C++, zrób to tak:

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

Jeśli potrzebujesz tych danych w buforze, aby je zmodyfikować, zrób to:

#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>()));
}
 137
Author: Björn Pollex,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-10-18 08:04:53
 sizeof(buffer) == sizeof(char*) 

Użyj zamiast tego długości.

Również lepiej użyć fopen z "wb"....

 13
Author: Alexey Sudachén,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-07-14 17:02:07

Oto krótki przykład, sposób C++ za pomocą rdbuf. Mam to z sieci. Nie mogę znaleźć mojego oryginalnego źródła na ten temat:

#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;
}
 13
Author: Thomas Matthews,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-01-13 18:16:02

Sizeof (buffer) to rozmiar wskaźnika w ostatniej linii, a nie rzeczywisty rozmiar bufora. Musisz użyć "długości", która została już ustalona

 6
Author: jcoder,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-03-24 14:01:24

Powinieneś przekazać length do fwrite zamiast sizeof (buffer).

 4
Author: retrodrone,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-03-24 14:02:02

Jest o wiele prostszy sposób. Nie obchodzi to, czy jest to plik binarny czy tekstowy.

Użyj 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];
 0
Author: Zeta,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-05-10 01:14:11

Można to zrobić za pomocą prostych poleceń w poniższym fragmencie.

Kopiuje cały plik o dowolnym rozmiarze. Brak ograniczeń rozmiaru!

Użyj tego. Sprawdzony I Działa!!
#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;
}

Posiadanie mniejszej wielkości bufora byłoby pomocne w kopiowaniu małych plików. Parzyste " bufor znakowy[2]" zrobi to.

 -1
Author: iMajetyHK,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-09-09 16:05:51