save things to file

i have a game i'm making. i need to save all the text that is on the screen to a file. how can i do this? it needs to be like how when you write a text document in the terminal u save all that text to a document. just simple like that. i can upload some code of my game too if needed
Are you using a screen manager like ncurses? If so, you talk to the screen manager and ask it for the screen content.
i am infact using ncurses. i dont know how to "talk to the screen manager and ask it for screen content"
http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/screen.html#SCREENDUMP

For the sake of time, I would suggest looking in the documentation before asking. ;)
Last edited on
i still dont get how to use putwin(). its not working. i need an example on how to save the screen to a file called "world" or maybe "world.txt" in the home folder
I came here from your other question [ http://www.cplusplus.com/forum/unices/126224/ ]...so if this looks like it's out of order...

Now, if I understand your two questions...
What if you used a two dimensional array that had the contents of what was at each "block" of the screen? Each type of object could have a different code (say unsigned ints to keep it simple and fast).
For example:
0 = nothing
1 = player
2 = block_type_1
3 = block_type_2
etc...

You could save using this format and I'm thinking it would be fairly simple to represent the contents onto the screen. Modifying a file that just had one array would take no time at all...
Last edited on
im really intereted in the link noxzema gave. if that works then i think that will be the solution. any idea on how putwin() and getwin() work? i mean the syntax of how to use it
agent047 its not anything great. its a terminal based game so i dont think it will be in your interest. im still learning. heres a video to one of the older versions of it tho https://www.youtube.com/watch?v=UoBozexLMxs still though can anyone help me with getwin() and putwin()? i think it will be the best way for my program to work but i cant figure out all the arguments for it and stuff
ok heres a link to the actual code. i think i have it set up like it shows how to use putwin() and getwin() on this link:
http://pubs.opengroup.org/onlinepubs/7908799/xcurses/putwin.html
now heres my code:
http://pastebin.com/2rCVgSkd
i put a note where i have the saving setup at. the file 'world' is in the same folder as the 'ncursesv6.cpp' file (home folder). it just isnt doing anything when i press 'k' to save or 'l' to load. can anyone fix it??? please ive literally been trying to get this saving to work for weeks
please anyone? im desparate for an answer
ok i have almost figured it out. i tried scr_dump() and scr_restore() and almost have it figured out. that problem is after you save it, and then load it back up, the screen shows the new characters from the saved file but doesnt update the properties. what i mean by that is, i have it made to where you cant walk through a '#'. after you load the saved file back up any '#' that just loaded up you can walk thru. i dont want you to be able to walk thru it after you load it back up. here is a little slip of my code...
1
2
3
4
5
6
7
  if(ch=='k') {
    scr_dump("world.dump");
  }
  if(ch=='l') {
    scr_restore("world.dump");
    doupdate();  //this is where it gets funky and needs to be fixed
  }

anyone got an idea? its been almost a week and i have no clue how to fix it. i think scr_restore is opening a new window maybe. help?
ok people i finally figured it out. after weeks of constant work on it i got it. so you first have to declare a file somewhere inside the loop. i did FILE * world; exactly like that, just below where i declared all of the varriables. then to save the file using putwin i used this code under an if statement:
1
2
3
world = fopen("world.win", "w");
putwin(stdscr, world);
fclose(world);

then under another if statement, with getwin i did:
1
2
3
4
5
delwin(stdscr);
world = fopen("world.win", "r");
stdscr = getwin(world);
fclose(world);
refresh();

things to note:
w in the putwin means the file is opening as a writable file. it also will create a new file in the process if the file world.win isnt already there. the r in getwin means its a readable file.
hope this helps someone! its a big relief for me lol
Topic archived. No new replies allowed.