Question on file io

Discussion in 'C++' started by carnie, Dec 29, 2003.

  1. carnie

    carnie Guest

    I have a BookData data structure that I'm trying to set up for file io. I
    open a file for random access io (same file for input and output), and
    write and read to the file with the following code.

    BookData book;
    long pos;

    getBook(&book);
    fstream data_file("books.dat",ios::in|ios::eek:ut);
    pos = LookUpBook(book) - sizeof(book);
    data_file.write((char *)&book,sizeof(book));

    The problem is that no changes I make to the variable book are reflected in
    the file. I'm using gcc 3.2.3 on a gentoo linux platform wit Reiser fs, if
    that makes any difference. Any and all help is appreciated.
    carnie :)
    carnie, Dec 29, 2003
    #1
    1. Advertising

  2. "carnie" <> wrote...
    > I have a BookData data structure that I'm trying to set up for file io. I
    > open a file for random access io (same file for input and output), and
    > write and read to the file with the following code.
    >
    > BookData book;
    > long pos;
    >
    > getBook(&book);
    > fstream data_file("books.dat",ios::in|ios::eek:ut);
    > pos = LookUpBook(book) - sizeof(book);
    > data_file.write((char *)&book,sizeof(book));
    >
    > The problem is that no changes I make to the variable book are reflected

    in
    > the file. I'm using gcc 3.2.3 on a gentoo linux platform wit Reiser fs, if
    > that makes any difference. Any and all help is appreciated.


    Without knowing what 'BookData' is or what 'LookUpBook' does, it would be
    a guessing game and not a technical discussion. So, if you want to play,
    here is my first guess: your data probably contains pointers, so when you
    write pointer values back into the file, the values stay the same and
    the changed memory pointed to by those pointers never gets written.

    Victor
    Victor Bazarov, Dec 29, 2003
    #2
    1. Advertising

  3. carnie

    Adie Guest

    carnie wrote:

    >The problem is that no changes I make to the variable book are reflected in
    >the file.


    Sounds like you may have issues related to "scope".

    >I'm using gcc 3.2.3


    Whilst it'll make no difference in respect to your problem - gcc is a
    C compiler, start using g++ now before you start hitting weird
    problems.
    Adie, Dec 29, 2003
    #3
  4. carnie

    Ron Natalie Guest

    "carnie" <> wrote in message news:Xns946196C2BDE0Ahillbilly@64.164.98.49...

    > getBook(&book);
    > fstream data_file("books.dat",ios::in|ios::eek:ut);
    > pos = LookUpBook(book) - sizeof(book);
    > data_file.write((char *)&book,sizeof(book));


    Every time you read or write the stream the "file position"
    is advanced by the amount red. You compute "pos" here
    but you omit any call that actually backs up the stream position
    so that you are overwriting the record you read in.
    Ron Natalie, Dec 29, 2003
    #4
  5. carnie wrote:
    > I have a BookData data structure that I'm trying to set up for file io. I
    > open a file for random access io (same file for input and output), and
    > write and read to the file with the following code.
    >
    > BookData book;
    > long pos;
    >
    > getBook(&book);
    > fstream data_file("books.dat",ios::in|ios::eek:ut);
    > pos = LookUpBook(book) - sizeof(book);
    > data_file.write((char *)&book,sizeof(book));
    >
    > The problem is that no changes I make to the variable book are reflected in
    > the file. I'm using gcc 3.2.3 on a gentoo linux platform wit Reiser fs, if
    > that makes any difference. Any and all help is appreciated.
    > carnie :)


    One more issue: the size of a structure may not be sum of
    the size of its members. The compiler is allowed to add padding
    bytes between members and perhaps some bookkeeping data at the
    end of the structure (or class).

    I suggest only using the write() for POD types. Otherwise, have
    members that write the structure to a stream:
    class Book
    {
    string title;
    string author;
    string publisher;
    string isbn;
    public:
    ostream& binary_write(ostream& out) const;
    };

    ostream&
    Book ::
    binary_write(ostream& out)
    {
    unsigned int length;
    length = title.size();
    out.write((unsigned char *) &length, sizeof(length));
    out.write(title.c_str(), length);
    length = author.size();
    out.write((unsigned char *) &length, sizeof(length));
    out.write(author.c_str(), length);
    // etc.
    return out;
    }

    In addition, I suggest a binary read method as well.
    I've found that writing to buffers is more efficient
    since it only uses one write for the entire buffer.
    This requires more code though.

    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.raos.demon.uk/acllc-c /faq.html
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
    http://www.sgi.com/tech/stl -- Standard Template Library
    Thomas Matthews, Dec 30, 2003
    #5
    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. Luis Esteban Valencia Muñoz
    Replies:
    3
    Views:
    1,404
    Scott Allen
    Jun 4, 2005
  2. key9
    Replies:
    7
    Views:
    374
    Pete Becker
    Sep 13, 2006
  3. Dave
    Replies:
    1
    Views:
    990
    Juan T. Llibre
    Jun 8, 2007
  4. =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki

    Reading of file by next of map file and by next of file descriptor.

    =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki, Jul 10, 2007, in forum: C++
    Replies:
    1
    Views:
    786
    Alf P. Steinbach
    Jul 10, 2007
  5. scad
    Replies:
    4
    Views:
    948
    James Kanze
    May 28, 2009
Loading...

Share This Page