Ver Mensaje Individual
  #2 (permalink)  
Antiguo 31/05/2013, 05:09
KikoSalinas
 
Fecha de Ingreso: mayo-2013
Ubicación: Granada
Mensajes: 8
Antigüedad: 11 años, 3 meses
Puntos: 0
Respuesta: Ficheros binarios

Código C++:
Ver original
  1. #include <iostream>
  2. #include <fstream>
  3. #include <stdlib.h>
  4.  
  5. using namespace std;
  6.  
  7.  
  8. int main (int argc, char **argv)
  9. {
  10.     const int TAM_BUFFER = 512;
  11.     char buffer [TAM_BUFFER];  
  12.  
  13.    
  14.     ifstream fi;
  15.     ofstream fo;
  16.  
  17.    
  18.     if (argc != 2) {
  19.         cerr << "Error: Numero de argumentos incorrecto\n";
  20.         cerr << "Formato: " << argv[0]<< " <fich_in> <fich_out>\n";
  21.         exit (1);
  22.     }
  23.    
  24.     fi.open (argv[1]);
  25.    
  26.     if (!fi) {
  27.         cerr << "Error: no pudo abrirse " << argv[1] << endl;
  28.         exit (2);
  29.     }
  30.    
  31.     fo.open(argv[2], ios::binary);
  32.    
  33.     if (fo.fail())
  34.     {
  35.         cerr << "Error: No pudo crearse " << argv[2] << endl;
  36.         exit (2);
  37.     }
  38.  
  39.    
  40.     while (fi.read(reinterpret_cast<char*>(buffer), TAM_BUFFER))
  41.         fo.write(buffer, TAM_BUFFER);
  42.  
  43.     fo.write(buffer, fi.gcount());
  44.  
  45.    
  46.     fi.close ();
  47.     fo.close ();
  48.  
  49.     return (0);
  50. }