0

I cannot hand over my fstream to a function I had created to write text into a textfile.

The fstreams I want to use (for now i only want to use usernameFile):

fstream passwordFile;
fstream usernameFile; 
fstream nameFile;

the function where I want to use the "fstream":

void writer(fstream fileToWrite, string stringToWrite, string fileName)
{
    fileToWrite.open(fileName, ios::app);
    if(fileToWrite.is_open())
    {
        fileToWrite << stringToWrite << endl;
    }
}

The function from where i call the "writer"-function to where i need to hand over the "usernameFile" fstream

void actionLogin() //loginprocedure
{
    string username;
    string password;

    cout << "Type in your Username" << endl;
    cin >> username;
    system("cls");
    cout << "Type in your Password" << endl;
    cin >> password;
    system("cls");
    cout << "Starting verification" << endl;
    
    reader(usernameFile, username, "UsernameSaveFile");

}

where i want to hand over:

Errormessage

Hope anyone can halp me :)

1 Answers1

3

Your error message is telling you that you cannot copy a std::fstream object. To resolve this, you can take it by reference in writer; this is as simple as adjusting the function to take fstream &fileToWrite, i.e.

void writer(fstream &fileToWrite, string stringToWrite, string fileName);

The same is true of the reader method, for the same reasons.

mitch_
  • 1,048
  • 5
  • 14
  • 1
    In fact, all three parameters should be passed by reference (the last two by `const` reference). – TonyK Jul 13 '22 at 10:52
  • Ooooh ok, thank you! Yeah just niticed, that I copied the code of the wrong function (writer instead of reader ) but works with both ig :) – Robin Kaiser Jul 13 '22 at 11:42