Integers input/output to txt | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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 ?

24th Jun 2016, 1:44 PM
DarKLighT
DarKLighT - avatar
2 Answers
+ 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.
24th Jun 2016, 7:20 PM
Stefan
Stefan - avatar
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
24th Jun 2016, 5:59 PM
Garme Kain
Garme Kain - avatar