#Won't write to file after getline()

1 messages · Page 1 of 1 (latest)

sullen eagleBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

proven basalt
#

you need to give it a mode i believe so it would be std::fstream file("password.txt", file.in | file.out);

normal yarrow
#

Perhaps just for my own curiosity, where in the opened file did you hope file<<encode(Password,Password.length()); would actually write to?

#

Wonder if the getline is causing a badbit or failbit which needs to be cleared.

#

what happens if after the getline and before the file << encode..., you call file.clear();?

proven basalt
#

That's just intellisense, if the file doesn't exist you also need std::fstream::app

#

std::fstream file("password.txt", std::fstream::in | std::fstream::out | std::fstream::app);

#

or std::ios::in/out/app

proven basalt
#

You sure the app is looking in the write place then?

#

What are you using for your IDE?

proven basalt
#

Well using std::fstream file("password.txt", std::ios::in | std::ios::out | std::ios::app);
Should always make sure that the file is opened or makes a new file. so you know if it makes a new empty file you'll have put it in the wrong place

proven basalt
#
int main() {
    //open the file
    std::fstream file("password.txt", std::ios::in | std::ios::out | std::ios::app);
    std::string Password;
    //get the first line of the file
    std::getline(file, Password, '\n');

    file.clear();
    file.seekp(0, std::ios::end);
    file << encode(Password, Password.length());
}
#

That should work