Lettura binaria di vector<string>

M

misirion

Ciao,
vorrei poter scrivere e leggere su files binari dei vettori di
stringhe, ed avrei implementato questo codice:

ifstream fin(conf.c_str(),ios::binary);
char inc[100];
fin.read( inc, sizeof(levelfiles) );
levelfiles = fromCharS(inc);
fin.close();

..... dove fromCharS:

vector<string> fromCharS(char* in) {
vector<string> o;
vector<string>* tmp = new vector<string>;
tmp = (vector<string> *)in;
for (int i=0;i<(int)tmp->size();i++)
o.push_back( (*tmp) );
return o;
}


Vorrei un vostro parere, se è una soluzione troppo sporca e
soprattutto perchè non funziona visto che il push_back genera una
violazione di accesso...

Grazie e ciao
 
M

misirion

In data Sat, 12 Feb 2005 12:19:32 -0500, Victor Bazarov
misirion said:
Ciao,
vorrei poter scrivere e [...]

This NG is English-speaking. You probably wanted to post
to it.comp.lang.c++

Sorry, I wrote in the wrong NG. However, I translate:


I'd like to read and write on binary files some vectors of strings, and I
wrote this code:

ifstream fin(conf.c_str(),ios::binary);
char inc[100];
fin.read( inc, sizeof(levelfiles) );
levelfiles = fromCharS(inc);
fin.close();

..... where fromCharS:

vector<string> fromCharS(char* in) {
vector<string> o;
vector<string>* tmp = new vector<string>;
tmp = (vector<string> *)in;
for (int i=0;i<(int)tmp->size();i++)
o.push_back( (*tmp) );
return o;
}


I'd like to know what you think, if this solution is too dirty and most of
all why it doesn't work: the push_back generates an access violation...

Thank you and bye...
 
V

Victor Bazarov

misirion said:
[..]
I'd like to read and write on binary files some vectors of strings, and I
wrote this code:

ifstream fin(conf.c_str(),ios::binary);
char inc[100];
fin.read( inc, sizeof(levelfiles) );

I think this is supposed to be

fin.read(inc, sizeof(inc));

, no?
levelfiles = fromCharS(inc);
fin.close();

.... where fromCharS:

vector<string> fromCharS(char* in) {

I recommend changing the argument to 'const char* in'.
vector<string> o;
vector<string>* tmp = new vector<string>;
tmp = (vector<string> *)in;

This is totally bogus, sorry. Casting a pointer to char to a pointer
to a vector is nonsensical. Besides, you're losing the pointer you
just obtained from the free store, which leads to a memory leak.

What are you trying to do? If you want to read all lines in a file
into a vector of strings, there are known solutions (which you can
find in the archives) and they don't involve casting or using 'get'.
They usually involve 'getline'. Search http://groups.google.com for
them.
for (int i=0;i<(int)tmp->size();i++)
o.push_back( (*tmp) );
return o;
}


I'd like to know what you think, if this solution is too dirty and most of
all why it doesn't work: the push_back generates an access violation...


The part where you cast a pointer to char to a pointer to a vector of
strings makes /no sense/. That's why it doesn't work. Dirty? I have
no idea what meaning you put into that. Not working, plain and simple.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,201
Messages
2,571,049
Members
47,652
Latest member
Campbellamy

Latest Threads

Top