fstream c ++ error

2

I have this piece of code,

void saveGame(vector<Usuario *> vector) {
    fstream file_obj;
    file_obj.open("users.dat");
    for(Usuario *u: vector){
        string auxName,auxPassword;
        auxName = u->getName();
        auxPassword = u->getPassword();
        //file_obj << auxName +" "+auxPassword;
        file_obj << "Hola";
    }
    file_obj.close();
}

The issue is that I do not record anything in the file, I have tried commenting and writing the "hello" but it does not say it either. Someone can give me a cable?
Thank you very much in advance

    
asked by Sergislabgg 13.05.2018 в 00:28
source

1 answer

3

you would need to indicate that the fstream opens for output

2 options:

  • using ofstream
  • using fstream with flag out

savefile.cpp

#include <iostream>
#include <fstream>
#include <stdio.h>

using namespace std;

int main() {

  ofstream archivo;
  archivo.open("salida.txt");
  archivo<<"hola"<<std::endl;
  archivo.close();

  fstream otroArchivo;
  otroArchivo.open("salida2.txt", ios::out);
  otroArchivo<<"Hola! 2"<<std::endl;
  otroArchivo.close();
  return 0;
}
    
answered by 13.05.2018 / 01:07
source