Bartek Kordalski
Bartek Kordalski

Reputation: 25

C++ Why in one case saving to file is working and another not?

Need some help over here, can somone explain me why this is working :

void change_boss()
    {
        string password;
        fstream file;



        cout << "Type new password" << endl;
        cin >> password;
        file.open("admin_list.txt");
        file << password;
        file.close();

    };

and this is not working ?

void change_worker()
    {
        string pass;
        fstream file;


        cout << "Type new password" << endl;
        cin >> pass;
        file.open("worker_list.txt");
        file >> pass;
        file.close();

    };

Any idea ? Coz I have no idea whats wrong with that

Upvotes: 0

Views: 31

Answers (1)

bfoster
bfoster

Reputation: 79

You are using the wrong operator; Your second block should instead replace this:

file >> pass;

with:

file << pass;

If you do not need both read and write access to a file, please instead consider using std::ifstream and std::ofstream for read-only and write-only operations respectively.

Upvotes: 2

Related Questions