Foros del Web » Programación para mayores de 30 ;) » C/C++ »

Lectura/Escritura de datos

Estas en el tema de Lectura/Escritura de datos en el foro de C/C++ en Foros del Web. He hecho este programa de prueva para escribir y leer un int, un float y un byte: @import url("http://static.forosdelweb.com/clientscript/vbulletin_css/geshi.css"); Código C++: Ver original #include <iostream> ...
  #1 (permalink)  
Antiguo 20/04/2009, 09:49
 
Fecha de Ingreso: abril-2009
Mensajes: 9
Antigüedad: 15 años, 1 mes
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
  #2 (permalink)  
Antiguo 20/04/2009, 10:59
boli-sp
Invitado
 
Mensajes: n/a
Puntos:
Respuesta: Lectura/Escritura de datos

para escritura puedes utilizar:

Código:
ofstream fichero;
fichero.open("fichero.txt");

fichero << variable1;
para lectura:
Código:
ifstream fichero;
fichero.open("fichero.txt");

fichero >> variable1;
recuerda que siempre hay que cerrar un archivo después de abrirlo:
Código:
fichero.close();
  #3 (permalink)  
Antiguo 20/04/2009, 12:36
 
Fecha de Ingreso: abril-2009
Mensajes: 9
Antigüedad: 15 años, 1 mes
Puntos: 0
Respuesta: Lectura/Escritura de datos

Gracias por la respuesta boli-sp.
He hecho el programa de prueba según la forma que me has indicado:
Código C++:
Ver original
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. int main() {
  7.     int a = 8765;
  8.     float b = 1.2345;
  9.     unsigned char c = 255;
  10.     ofstream ou2("2.txt", ios::out | ios::binary);
  11.     ou2 << a;
  12.     ou2 << b;
  13.     ou2 << c;
  14.     ou2.close();
  15.     //
  16.     ifstream in2("2.txt", ios::in | ios::binary);
  17.     int r; float t; unsigned char y;
  18.     in2 >> r;
  19.     in2 >> t;
  20.     in2 >> y;
  21.     in2.close();
  22.     cout << r << endl;
  23.     cout << t << endl;
  24.     cout << (int) y << endl;
  25. }
Sin embargo lo que pasa es que me lo escribe en formato texto y al leerlo no lo hace bien.

Última edición por TinoFDW; 20/04/2009 a las 12:46
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 21:37.