Question on file io

C

carnie

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 :)
 
V

Victor Bazarov

carnie said:
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
 
A

Adie

carnie said:
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.
 
R

Ron Natalie

carnie said:
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.
 
T

Thomas Matthews

carnie said:
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
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top