+ 6

How can I write variables to .txt files?

I am trying to make a program that saves changes or progress, by using fstream. I searched online and found codes that were similar to what I need, but, I was not able to get what I wanted. Here is the code or "puesdo code" that I worked from. If you know how to save or store variables using fstream, I would appreciate your input. https://code.sololearn.com/c475wbkF190n/?ref=app

10th Jun 2017, 3:55 PM
Manual
Manual - avatar
26 ответов
+ 5
It will be best to have 3 functions here, create, read and write... I also use this function for checking if the file exists or not: bool exist(string path) { ifstream file; file.open(path.c_str()); return bool(file); } Create: this will create a new file only if the player's data doesn't exist in the database... void Create(string path) { if(!exist(path)) { fstream fout; fout.open(path.c_str()); fout<<1<<" "<<100<" "<<0<<endl; fout.close(); } } Read: This will simply read the data from the save files... void Read(string path,int& lvl,int& health,int& progress) { ifstream fin; fin.open(path.c_str()); fin>>lvl>>health>>progress; fin.close(); } Write: This will overwrite the data to the file... void write(string path,int lvl,int health,int progress) { ofstream fout; fout.open(path.c_str(),ios::trunc); fout<<lvl<<" "<<health<<" "<<progress; fout.close(); } This may help you...
10th Jun 2017, 4:41 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 9
@Kinshuk @Manual Guys, sorry for spamming, but you've just asked something similar to what I wanted to know. How do we append data to a file, without overwriting it? In c, I would do something like: FILE *f; f= fopen ("fileName.txt" , at); And I would do my thing with it... What is the equivalent in c++? Do you maybe know?
10th Jun 2017, 6:26 PM
dρlυѕρlυѕ
dρlυѕρlυѕ - avatar
+ 4
@Manual, what if there are more than one player in the game? For the given scenario binary mode should be more suitable. Moreover, it would be better reuse class or structure to represent your player.
10th Jun 2017, 4:41 PM
देवेंद्र महाजन (Devender)
देवेंद्र महाजन (Devender) - avatar
+ 4
@Devender You are right. I am tring to save or write small things, before I write structures and or classes into files. I will do that in a future project.
10th Jun 2017, 4:45 PM
Manual
Manual - avatar
+ 4
@Manual check this code. Does is it helpful? https://code.sololearn.com/c6JRw8grXTk5/#cpp
11th Jun 2017, 8:24 AM
देवेंद्र महाजन (Devender)
देवेंद्र महाजन (Devender) - avatar
+ 3
So you simply want to read and write data from a file which can be altered even at runtime? In my current Airline Reservation Program, I save the passenger data and the seats booked in a particular flight on a particular day in files so that the program may have access to these changes later as well... In the seats part, for example, this helps me see if the flight is completely booked or not... Do you need to achieve a similar thing?
10th Jun 2017, 4:09 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 3
Here are two references from a C++ forum. about loading highscores: http://www.cplusplus.com/forum/beginner/130571/
10th Jun 2017, 4:24 PM
Manual
Manual - avatar
+ 3
@Manual If you have checked this: https://code.sololearn.com/cLGN9A6ZdaHB/?ref=app You can see that I have used similar functions like the one I gave you to maintain the game data... //Sorry, but I am not trying to advertise my code, just sharing it as it uses the same thing... //If you feel this as advertising, please let me know, Ill remove this answer then...
10th Jun 2017, 5:07 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 3
Here is the new version with the c and c++ bool headers so it compiles. @Kinshuk Thanks again! https://code.sololearn.com/cKXNrY5ZRufU/#cpp
10th Jun 2017, 6:24 PM
Manual
Manual - avatar
+ 3
@dplusplus I believe there is an append perameter that you can use. I am still working on files and have not tried it I will add my reference to it.
10th Jun 2017, 6:36 PM
Manual
Manual - avatar
+ 3
@dplusplus You are welcome! Please let me know what you get from it. I will be using those methods for files as well.
10th Jun 2017, 6:56 PM
Manual
Manual - avatar
+ 2
edit Please post your references, if you are able. I will post some as well.
10th Jun 2017, 4:13 PM
Manual
Manual - avatar
+ 2
@Manual By sources, do you mean codes or reference sites?
10th Jun 2017, 4:14 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 2
@Kinshuk You got it, I am tring to make a code that can be altered at run time. I am thing to find a way to store strings, ints and other values, alter them. Then call the values, when I run the code again.
10th Jun 2017, 4:20 PM
Manual
Manual - avatar
+ 2
Here is the link for the tutorial on file handling. http://www.cplusplus.com/doc/tutorial/files/
10th Jun 2017, 4:36 PM
Manual
Manual - avatar
+ 2
@Manual And do note that path can be the username of the player, as it must be unique for all players to maintain data properly...
10th Jun 2017, 4:44 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 2
@Kinshuk No, I appreciate that you shared your code to help me. I do not feel it is advertising. Thank you,for sharing it. I recently started windows programming, and getting the file down should help.
10th Jun 2017, 6:11 PM
Manual
Manual - avatar
+ 2
@Kinshuk Thank you very much! I finished debugging the code and it compiled.
10th Jun 2017, 6:18 PM
Manual
Manual - avatar
+ 2
@dplusplus Here is one, I hope it helps. http://www.cplusplus.com/reference/fstream/ofstream/open/
10th Jun 2017, 6:41 PM
Manual
Manual - avatar
+ 2
@Manual You're Welcome! Glad to know that I could be helpful.
11th Jun 2017, 1:06 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar