C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Question on file io

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
carnie
Guest





PostPosted: Mon Dec 29, 2003 3:32 am    Post subject: Question on file io Reply with quote



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::out);
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 Smile
Back to top
Victor Bazarov
Guest





PostPosted: Mon Dec 29, 2003 3:44 am    Post subject: Re: Question on file io Reply with quote



"carnie" <fred_bates (AT) sbcglobal (DOT) net> wrote...
Quote:
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::out);
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



Back to top
Adie
Guest





PostPosted: Mon Dec 29, 2003 8:23 am    Post subject: Re: Question on file io Reply with quote



carnie wrote:

Quote:
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".

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

Back to top
Ron Natalie
Guest





PostPosted: Mon Dec 29, 2003 12:58 pm    Post subject: Re: Question on file io Reply with quote


"carnie" <fred_bates (AT) sbcglobal (DOT) net> wrote


Quote:
getBook(&book);
fstream data_file("books.dat",ios::in|ios::out);
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.


Back to top
Thomas Matthews
Guest





PostPosted: Tue Dec 30, 2003 1:21 am    Post subject: Re: Question on file io Reply with quote

carnie wrote:
Quote:
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::out);
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 Smile

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


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.