Ver Mensaje Individual
  #1 (permalink)  
Antiguo 20/04/2009, 09:49
TinoFDW
 
Fecha de Ingreso: abril-2009
Mensajes: 9
Antigüedad: 15 años, 2 meses
Puntos: 0
Lectura/Escritura de datos

He hecho este programa de prueva para escribir y leer un int, un float y un byte:
Código C++:
Ver original
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. int main() {
  7.     ofstream ou("1.txt", ios::out | ios::binary);
  8.     int a = 8765;
  9.     float b = 1.2345;
  10.     unsigned char c = 255;
  11.     ou.write(reinterpret_cast<char *>(&a), sizeof(int));
  12.     ou.write(reinterpret_cast<char *>(&b), sizeof(float));
  13.     ou.write(reinterpret_cast<char *>(&c), sizeof(unsigned char));
  14.     ou.close();
  15.     //
  16.     ifstream in("1.txt", ios::in | ios::binary);
  17.     int q; float w; unsigned char e;
  18.     in.read(reinterpret_cast<char *>(&q), sizeof(int));
  19.     in.read(reinterpret_cast<char *>(&w), sizeof(float));
  20.     in.read(reinterpret_cast<char *>(&e), sizeof(unsigned char));
  21.     in.close();
  22.     cout << q << endl;
  23.     cout << w << endl;
  24.     cout << (int) e << endl;
  25. }
Y funciona, pero tengo la sensación que hay algo poco eficiente, sobretodo para el byte.
¿Hay alguna otra forma mejor para trabajar con archivos y datos?

Última edición por TinoFDW; 20/04/2009 a las 12:44