Write a file

In my game i would like to have a function that writes the balance of the player to a file, i have already made a file and got a function to read it and put it into a variable put i need help with changing the file when i call this new function

I basically want to overwrite the file completely but i dont know how
Last edited on
By default ofstream creates a new file (even if one exists). If you wanted to append you'd pass ios::app.
Okay so how would I overwrite it? I know how to read from the file, open it and stuff but no idea how to re write it
I suppose you could use the same function to write to file the second time as the first.

1
2
3
4
5
6
7
8
9
10
11
12
ofstream myOutFile("file.txt"); // create output file
myOutputFunction(myObject,myOutFile); // output object to file

ifstream myFile("file.txt"); // open input file
myInputFunction(myObject,myOutFile); // do read operations
myFile.close(); // close file

// Make changes to object

ofstream myOutFile("file.txt"); // create new output file (overwrites)
myOutputFunction(myObject,myOutFile); // output object changes to file
myOutFile.flush();

By default, an std::ofstream opens a file with std::ios::out, which essentially deletes the contents of the file, and provieds a stream that can be written to. This will provide the function you want.

Example:

1
2
3
4
std::string a_file{"whatever you want"};
std::ofstream out{a_file.c_str(), std::ios::out};
//now if you want to close it, the contents of the file are erased.
//this does not remove the file itself, though. 


You can read more on std::ofstream here: http://en.cppreference.com/w/cpp/io/basic_ofstream

Note that if you want to delete a file, you will have to use the operating system's API. However, the Boost library provides a cross-platform solution that I find much more preferable.

http://www.boost.org/doc/libs/1_58_0/libs/filesystem/doc/reference.html#remove
I have a problem: how would i do it if all i want to do is write a number? Because all i want it to do is write the balance of the customer to a .txt file, so it is stored and can be accessed if the program is exited and then run again
I worked it out, cheers guys

1
2
3
4
5
6
7
8
9
void Basic::AutoSave()
{
	std::remove("save.txt");
	std::ofstream save("save.txt");
	save << b.balance << std::endl;
	save.close();

	return;
}
For completeness: remove, ostream::close and the return statement are all unnecessary. The following code will perform exactly the same operation.

1
2
3
4
5
void Basic::AutoSave()
{
	std::ofstream save("save.txt");
	save << b.balance << std::endl;
}


1. As Texan40 and IWishIKnew said, by default an ofstream is opened for overwrite (you only have to do something if you want to append to the existing file.)
2. So removing the file is not necessary (you're getting the o/s to do work unnecessarily.)
3. And ofstream::close() is called by the ofstream destructor.
4. And return; is not needed (as there's nothing to return.)

OK, so all these issues are benign. But when coding you should be aiming for the minimal and complete solution.

Apart from anything else, I'm far too lazy to type unnecessary code! :-)

Andy

PS If I was going to add code to this function, it would be for error handling.
Last edited on
Okay cheers
Topic archived. No new replies allowed.