writing vector of structures to a file

U

uche

can someone tell me if this is a correct way to writing a vector of
structure to a file...
will this write the contents of a vector into the file?

bool Cwrite (vector <struct> &write, const string & filename)
{
ofstream myfile (filename);
if (myfile.is_open())
{
myfile << write;
myfile.close();
return true;
}
else
{
cout << "Unable to open file";
return false;
}
}
 
B

bnonaj

uche said:
can someone tell me if this is a correct way to writing a vector of
structure to a file...
will this write the contents of a vector into the file?

bool Cwrite (vector <struct> &write, const string & filename)
{
ofstream myfile (filename);
if (myfile.is_open())
{
myfile << write;
myfile.close();
return true;
}
else
{
cout << "Unable to open file";
return false;
}
}
What I did was create a template function to handle stream output of a
vector, then as long as you have the stream output implemented for the
value_type of the vector it'll work. Generally you shouldn't close the
stream when overloading the << operator for streams and should return a
reference to the original stream, (see below for general form).

std::eek:stream &operator << (std::eek:stream &str, const MyObj &inp)
{
// Implementation
return str;
}

JB
 
I

Ian Collins

uche said:
can someone tell me if this is a correct way to writing a vector of
structure to a file...
will this write the contents of a vector into the file?

bool Cwrite (vector <struct> &write, const string & filename)
{
ofstream myfile (filename);
if (myfile.is_open())
{
myfile << write;
myfile.close();
return true;
}
else
{
cout << "Unable to open file";
return false;
}
}
How many error messages did you get when you tried to compile it?

There isn't an operator << for std::vector.
 
J

Jim Langston

uche said:
can someone tell me if this is a correct way to writing a vector of
structure to a file...
will this write the contents of a vector into the file?

bool Cwrite (vector <struct> &write, const string & filename)
{
ofstream myfile (filename);
if (myfile.is_open())
{
myfile << write;
myfile.close();
return true;
}
else
{
cout << "Unable to open file";
return false;
}
}

You have to somehow iterate through the vector. Personally, I use a for
loop although there are other ways.

// ...
if ( myfile.is_open() )
{
for ( std::vector<struct>::iterator it = write.being(); it !=
write.end; ++it )
myfile << (*it) << "\n";
myfile.close();
return true
}
// ...
 
M

Marcus Kwok

Jim Langston said:
You have to somehow iterate through the vector. Personally, I use a for
loop although there are other ways.

// ...
if ( myfile.is_open() )
{
for ( std::vector<struct>::iterator it = write.being(); it !=
write.end; ++it )
myfile << (*it) << "\n";
myfile.close();
return true
}
// ...

If I'm feeling froggy, I'll copy the vector to the ostream:

copy(write.begin(), write.end(), ostream_iterator<struct>(myfile, "\n"));
 
J

James Kanze

What I did was create a template function to handle stream output of a
vector, then as long as you have the stream output implemented for the
value_type of the vector it'll work.

How? Or rather, where? If you put it in the global namespace,
it won't be found during type dependant name lookup in template
code (e.g. in an ostream_iterator). And if you put it in std::,
you're violating the standard (although it will in fact almost
certainly work).

But the real question is what happens when two people on your
project do the same thing, each one using a different format?
Generally you shouldn't close the
stream when overloading the << operator for streams and should return a
reference to the original stream, (see below for general form).

Yes, but he was writing a function which wrote the complete
file. (It also took the filename, and did the open.)

Whether this is a good idea is, of course, another question, but
I suppose that there are contexts where it is valid.
 
B

bnonaj

James said:
How? Or rather, where? If you put it in the global namespace,
it won't be found during type dependant name lookup in template
code (e.g. in an ostream_iterator). And if you put it in std::,
you're violating the standard (although it will in fact almost
certainly work).

But the real question is what happens when two people on your
project do the same thing, each one using a different format?
If code is being repeatedly designed by separate programmers on a
project, I would suggest that the project is not being well managed.
Yes, but he was writing a function which wrote the complete
file. (It also took the filename, and did the open.)

Whether this is a good idea is, of course, another question, but
I suppose that there are contexts where it is valid.
Maybe, but if that behaviour is not what is usually expected then care
must be taken to ensure that this is made explicit, to avoid potential
problems with future code development.
--
James Kanze (GABI Software) email:[email protected]
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Finally, all the things you mention are Quality Assurance issues that
should be taken care of with good management.

JB
 

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
473,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top