Can't write a map on file// Can't read neither.

Hi!
It's my first question, so i thinks it's gonna be kinda messy. My english it's not the best, but i think somebody could understand me.

Problem:
I have a map, and i want to write both field of the map in a file.
I only can do that for the first item, next time i want to enter a new object, the program crashes.

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
/// THIS IS MY ´"WRITE" FUNCTION.
void ingresar(int referencia, Persona per) {

	ofstream escritura;
	escritura.open("agenda.dat", ios::app | ios::binary);
	if (escritura.fail()) {
		cout << "\nError al intentar abrir ESCRITURA\n";
	}

	map <int, Persona> buffer;
	map <int, Persona>::iterator it;

	buffer.insert(pair <int,Persona> (referencia, per));
	it = buffer.begin();

	escritura.write((char*)&it, sizeof(buffer));
	escritura.close();

}
////THIS IS MY READ FUNCTION.
void leer() {

	ifstream lectura;
	lectura.open("agenda.dat", ios::in | ios::binary);
	if (lectura.fail()) {
		cout << "\nERROR EN LECtURA DE ARCHIVO\n";
	}

	map <int, Persona> buffer;
	map <int, Persona>::iterator it;

	while (!lectura.eof()) {
		lectura.read((char*)&it, sizeof(buffer));
		if (!lectura.eof()) {
			it->second.getPersona();
		}

	}
	lectura.close();

}


i'm already lost and i don't know what the problem is. after the first element, everything crash.
Any suggestion?

again, sorry for my bad english and i, its kinda obvious, but i'm a beginner/noob.
Do not attempt to write the raw bytes of the map iterator. Work with C++. Write the variables how they want to be written. Let the language work with you.

Your code appears to be trying to write the iterator to file; this makes no sense. The iterator is not the data you care about. The iterator is a way to reach the data you care about.

What is a Persona object?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/// THIS IS MY ´"WRITE" FUNCTION.
void ingresar(int referencia, Persona per) {

	ofstream escritura;
	escritura.open("agenda.dat", ios::app | ios::text);
	if (escritura.fail()) {
		cout << "\nError al intentar abrir ESCRITURA\n";
	}

	map <int, Persona> buffer;
	map <int, Persona>::iterator it;

	buffer.insert(pair <int,Persona> (referencia, per));
	it = buffer.begin();

	escritura << (*it).first; // Write out the int value, which was referencia, to file

        // What is inside a Persona object? Need to write out the values of a Persona
	
}


Right now there is no reason at all to create a map. I assume you intend there to be a map from somewhere else later containing many values?
Last edited on
hi, first of all, thanks for your response: the iterator part is awesome for me now, because the fact that is pointing at the data and it's not the data itself, it's kinda obvious but i didn't realize that hours ago.

i need a map because:
in the first field, i have an integer reference. and i need to store that.
in the second field, i have an object called "Persona". (name and last name).

but i need to store a lot of objects and, afterwards, i'll have to modify that objects accessing through the reference.

This is my Class Persona code:

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
class Persona {
private:
	string nombre, apellido;
public:
	Persona() {}
	~Persona() {}
	//SETERS
	void setNombre(string nombre) {
		this->nombre = nombre;
	}
	void setApellido(string apellido) {
		this->apellido = apellido;
	}

	void setPersona() {
		cout << "\nNombre: ";
		getline(cin, nombre);
		cout << "Apellido: ";
		getline(cin, apellido);
		cout << endl;
	}
	//GETERS
	string getNombre() {
		return nombre;
	}
	string getApellido() {
		return apellido;
	}
	void getPersona() {
		cout << "\nNombre: " << nombre;
		cout << "\nApellido: " << apellido;
	}
};


and the reason i need a map, it's because the file i'm intended to write, it's like an agenda (with names, last names and phones).
in other part of my code (in another project, because this part won't budge) i have an index, with 2 fields:
map <int,int> index;

first field: phone number;
second field: reference number;

so, the reference here (second field) is the same reference i use in the first field of my function void Ingresar().
and then, if i want to access at some data in the file, i only need the reference.


maybe it's nonsense, and i know it might be. plus, my english is very sad.
sorry for the lame explanation and i understand if you can't help me anymore.

thanks!
Persona is entirely defined by two string values. You just need to store those two strings. Put a line ending between each output value so you can easily read them back in from file. Remember to set the file out to write the data as text; not binary.

1
2
3
4
5
6
    escritura << (*it).first; // Write out the int value, which was referencia, to file
    escritura << '\n';
    escritura << (*it).second.nombre; 
    escritura << '\n';
    escritura << (*it).second.apellido;
    escritura << '\n';


Last edited on
i'm trying that solution and i think its working.

thanks man!
thanks
Topic archived. No new replies allowed.