Ver Mensaje Individual
  #4 (permalink)  
Antiguo 13/10/2010, 16:26
fightmx
 
Fecha de Ingreso: febrero-2003
Ubicación: D.F.
Mensajes: 163
Antigüedad: 21 años, 2 meses
Puntos: 22
Respuesta: Creando el contenido antes que el fichero

Te puede servir que utilices stringstream.

Ejemplo:

Código C++:
Ver original
  1. #include <iostream>
  2. #include <fstream>
  3. #include <sstream>
  4. using namespace std;
  5.  
  6.  
  7. class MyClass{
  8.     stringstream log;
  9. public:
  10.     void someFunction(){
  11.         // code...
  12.         // ouch!
  13.         log << "my error string 1 in someFunction()" << endl;
  14.         // code...
  15.         // ouch!
  16.         log << "my error string 2 in someFunction()" << endl;
  17.     }
  18.  
  19.     void anotherFunction(){
  20.         // code...
  21.         // ouch!
  22.         log << "my error string 1 in anotherFunction()" << endl;
  23.         // code...
  24.     }
  25.  
  26.     void saveErrors(const string& fname){
  27.         ofstream out(fname.c_str());
  28.         out << log.rdbuf();
  29.     }
  30. };
  31.  
  32. int main(){
  33.     MyClass mc;
  34.     mc.someFunction();
  35.     mc.anotherFunction();
  36.     mc.saveErrors("myErrorLog.txt");
  37.     return 0;
  38. }