0
Integers input/output to txt
How can i output an integer to a file and then input it from it ? and can i use this to do some primitive game-saves ?
2 Respuestas
+ 1
There is a more simple and commonly used way: the iostream library. The fstream header in the standard library was explicitly designed to provide char input / output of data in C++.
Here's an example :
#include <fstream>
int main() {
int i =10;
double d = 7.8897;
std::ofstream outputFile;
outputFile.open("mydata.txt");
if (outputFile) {
outputFile << "Hallo" ;
outputFile << i;
outputFile << d;
}
// For proper use you should check the file object regularly
outputFile.close();
std::ifstream inputfile;
inputFile.open("mydata.txt);
inputFile >> a;
//...
}
You might want to add space after the output so it it easier to read from the file.
0
You should convert the int to a char array. and then from char array to int.
For game saves you can use json or xml files. or in a plain txt as you've said