Connecting Tech Pros Worldwide Help | Site Map

Lettura binaria di vector<string>

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 23rd, 2005, 12:49 AM
misirion
Guest
 
Posts: n/a
Default Lettura binaria di vector<string>

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

  #2  
Old July 23rd, 2005, 12:49 AM
Victor Bazarov
Guest
 
Posts: n/a
Default 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++


  #3  
Old July 23rd, 2005, 12:49 AM
misirion
Guest
 
Posts: n/a
Default 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...
  #4  
Old July 23rd, 2005, 12:49 AM
Victor Bazarov
Guest
 
Posts: n/a
Default 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


  #5  
Old July 23rd, 2005, 12:49 AM
misirion
Guest
 
Posts: n/a
Default Re: Lettura binaria di vector<string>

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

Bye!
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.