writing vector of structures to a file

Discussion in 'C++' started by uche, Apr 2, 2007.

  1. uche

    uche Guest

    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;
    }
    }
     
    uche, Apr 2, 2007
    #1
    1. Advertising

  2. uche

    bnonaj Guest

    uche wrote:
    > 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
     
    bnonaj, Apr 2, 2007
    #2
    1. Advertising

  3. uche

    Ian Collins Guest

    uche wrote:
    > 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.

    --
    Ian Collins.
     
    Ian Collins, Apr 2, 2007
    #3
  4. uche

    Jim Langston Guest

    "uche" <> wrote in message
    news:...
    > 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
    }
    // ...
     
    Jim Langston, Apr 3, 2007
    #4
  5. uche

    Marcus Kwok Guest

    Jim Langston <> wrote:
    > "uche" <> wrote in message
    > news:...
    >> 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
    > }
    > // ...


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

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

    --
    Marcus Kwok
    Replace 'invalid' with 'net' to reply
     
    Marcus Kwok, Apr 3, 2007
    #5
  6. uche

    James Kanze Guest

    On Apr 3, 12:58 am, bnonaj <> wrote:
    > uche wrote:
    > > 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.


    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.

    --
    James Kanze (GABI Software) email:
    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
     
    James Kanze, Apr 4, 2007
    #6
  7. uche

    bnonaj Guest

    James Kanze wrote:
    > On Apr 3, 12:58 am, bnonaj <> wrote:
    >> uche wrote:
    >>> 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.

    >
    > 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.
    >> 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.

    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:
    > 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
     
    bnonaj, Apr 4, 2007
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. pmatos
    Replies:
    6
    Views:
    24,035
  2. Alfonso Morra
    Replies:
    11
    Views:
    740
    Emmanuel Delahaye
    Sep 24, 2005
  3. Replies:
    8
    Views:
    1,987
    Csaba
    Feb 18, 2006
  4. Javier
    Replies:
    2
    Views:
    603
    James Kanze
    Sep 4, 2007
  5. Rushikesh Joshi
    Replies:
    0
    Views:
    387
    Rushikesh Joshi
    Jul 10, 2004
Loading...

Share This Page