473,765 Members | 2,086 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(levelfil es) );
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
Jul 23 '05 #1
4 2052
"misirion" <mi******@freem ail.it> wrote...
Ciao,
vorrei poter scrivere e [...]


This NG is English-speaking. You probably wanted to post
to it.comp.lang.c+ +
Jul 23 '05 #2
In data Sat, 12 Feb 2005 12:19:32 -0500, Victor Bazarov
<v.********@com Acast.net> ha scritto:
"misirion" <mi******@freem ail.it> wrote...
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(levelfil es) );
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...
Jul 23 '05 #3
"misirion" <misirion@no_sp amyahoo.nospamm mmit> wrote...
[..]
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(levelfil es) );
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)[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...


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
Jul 23 '05 #4
Thank you, you helped me a lot.
Now my code works...

Bye!
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
5064
by: Gaurav | last post by:
Hello I have a program that basically inverts the contents of files except first line. It compiles fine but gives me core dump on running. If i comment temp.clear() it runs fine, but i need it to clear the temp vector for each file. ********************* code *******************
1
18006
by: Matt Garman | last post by:
What is the "best" way to copy a vector of strings to an array of character strings? By "best", I mean most elegantly/tersely written, but without any sacrifice in performance. I'm writing an application using C++ and the STL for handling my data. Unfortunately, I must interact with a (vanilla) C API. I use vectors of strings (for simplicity and less memory hassle), but the function calls for this API require arrays of character...
10
3100
by: dalbosco | last post by:
Hello, I am new to STL and I've written the following code which crash. Could anyone tell me why this code crash? Thanks by advance. -- J-F #include <iostream>
2
2635
by: ehui928 | last post by:
hi, everybody I am a newbie in STL. When I compile the following program under gcc4.0, I got a the following errors. I wonder whether the form of list< vector<string> > is correct in STL ? // test.cpp #include <iostream> #include <fstream> #include <vector> #include <string>
10
2498
by: Shafik | last post by:
Hello, I am new to C++. I know the reason is probably template instantiation problems ... but what's the *real* reason I cannot declare a: vector<stringv = vector<string>(4); Thanks! --Shafik
5
2461
by: Etrex | last post by:
Hello, This is my first attempt at a c++ program, and it is a long post, please bear with me. I'm trying to read in a text file containing a firewall log, make the information searchable by an element (protocol, remote ip etc). The file is of known max width (65) containing 8 columns of known max size. The columns are seperated by white space. However, it is of unknown length. I read that to input this file in, I should use a...
6
5716
by: arnuld | last post by:
This works fine, I welcome any views/advices/coding-practices :) /* C++ Primer - 4/e * * Exercise 8.9 * STATEMENT: * write a program to store each line from a file into a * vector<string>. Now, use istringstream to read read each line * from the vector a word at a time.
6
7384
by: Mr. K.V.B.L. | last post by:
I want to start a map with keys but an empty vector<string>. Not sure what the syntax is here. Something like: map<string, vector<string MapVector; MapVector.insert(make_pair("string1", new vector<string>)); MapVector.insert(make_pair("string2", new vector<string>)); MapVector.insert(make_pair("string3", new vector<string>));
42
4543
by: barcaroller | last post by:
In the boost::program_options tutorial, the author included the following code: cout << "Input files are: " << vm.as< vector<string() << "\n"; Basically, he is trying to print a vector of string, in one line. I could
0
9393
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10007
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9946
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8830
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7371
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5272
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5413
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3921
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2800
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.