Connecting Tech Pros Worldwide Forums | Help | Site Map

Lettura binaria di vector<string>

misirion
Guest
 
Posts: n/a
#1: Jul 23 '05
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)[i] );
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

Victor Bazarov
Guest
 
Posts: n/a
#2: Jul 23 '05

re: Lettura binaria di vector<string>


"misirion" <misirion@freemail.it> wrote...[color=blue]
> Ciao,
> vorrei poter scrivere e [...][/color]

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


misirion
Guest
 
Posts: n/a
#3: Jul 23 '05

re: Lettura binaria di vector<string>


In data Sat, 12 Feb 2005 12:19:32 -0500, Victor Bazarov
<v.Abazarov@comAcast.net> ha scritto:
[color=blue]
> "misirion" <misirion@freemail.it> wrote...[color=green]
>> Ciao,
>> vorrei poter scrivere e [...][/color]
>
> This NG is English-speaking. You probably wanted to post
> to it.comp.lang.c++
>[/color]

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)[i] );
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...
Victor Bazarov
Guest
 
Posts: n/a
#4: Jul 23 '05

re: Lettura binaria di vector<string>


"misirion" <misirion@no_spamyahoo.nospammmmit> wrote...[color=blue]
> [..]
> 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) );[/color]

I think this is supposed to be

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

, no?
[color=blue]
> levelfiles = fromCharS(inc);
> fin.close();
>
> .... where fromCharS:
>
> vector<string> fromCharS(char* in) {[/color]

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

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.
[color=blue]
> for (int i=0;i<(int)tmp->size();i++)
> o.push_back( (*tmp)[i] );
> 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...[/color]

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


misirion
Guest
 
Posts: n/a
#5: Jul 23 '05

re: Lettura binaria di vector<string>


Thank you, you helped me a lot.
Now my code works...

Bye!
Closed Thread