Save and load from external file

I want to save data from a 20 by 60 array (yes its multidimensional -.-"), as well as save specific data in a formatted type of way, so that the data can be easily retrieved. However, I do not know how to output the data into the text file in a formatted way, and neither do I know how to retrieve it.

I looked on the website for help, but most of them just ...write one thing in and closed. Most of the time they are also conveniently already in string. I'm not sure if outputting more things cause problems.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <fstream>
#include <istream>
#include <ostream>
#include "struct.h"


using namespace std;

extern _OVERALL_ hero;
char map[20][60];

void saving (int counter)
{
	ofstream save("save.txt");//open stream?

	save<<map;//save the map-
	save << "hero hp"<<hero.hp<<endl<<	//save hero stats		error here saying the same thing,
	"hero attack"<<hero.ability.atk<<endl<<						//as well as operand "expected a ;"
	"hero defend"<<hero.ability.def<<endl<<
	"hero agility"<< hero.ability.agi<<endl<<
	"counter" <<counter	<<endl<<
	"name"<<hero.name;

	save.close();//close connection

	
}

void loading ()
{
	unsigned char maze[20][60];	//array for map to be put into
	char buffer[1220];	//buffer

	ifstream load;	//open stream

	load.open ("save.txt");

	Maps.getline(buffer,sizeof (buffer),'!');	//Retrieve characters in map and give it to buffer up until ! mark

	//<----I don't know how retrieve specific information. at all.
}
Last edited on
See this:

http://www.cplusplus.com/reference/ostream/basic_ostream/operator%3C%3C/
http://www.cplusplus.com/reference/istream/basic_istream/operator%3E%3E/

for what the istream/ostream understand with their operators >>/<<
Unfortunately this

save<<map;//save the map-

does not do what you think it does. Due to this

basic_ostream& operator<< (void* val);

it saves only the pointer. You need a loop to store all elements of the array.


Byt the way: You can overload your own operator >>/<< like std::string does:

http://www.cplusplus.com/reference/string/string/operator%3E%3E/

I suggest that you use std:string. That makes things way easier
Last edited on
Topic archived. No new replies allowed.