473,320 Members | 1,694 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Question on file io

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 :-)
Jul 22 '05 #1
4 1293
"carnie" <fr********@sbcglobal.net> 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::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
Jul 22 '05 #2
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.
Jul 22 '05 #3

"carnie" <fr********@sbcglobal.net> wrote in message news:Xn***********************@64.164.98.49...
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.

Jul 22 '05 #4
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::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 :-)


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

Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Stevey | last post by:
I have the following XML file... <?xml version="1.0"?> <animals> <animal> <name>Tiger</name> <questions> <question index="0">true</question> <question index="1">true</question> </questions>
7
by: nospam | last post by:
Ok, 3rd or is it the 4th time I have asked this question on Partial Types, so, since it seems to me that Partial Types is still in the design or development stages at Microsoft, I am going to ask...
7
by: dan | last post by:
hey peeps, i am completely new at c++ and i need some help with an assignment. it is basically about file i/o with fstreams. i understand how to open a file with fstream, but how would you read,...
21
by: siroregano | last post by:
Hi Everyone- I'm new to this group, and almost-as-new to asking programming questions publicly, so please forgive me if I miss a convention or two! I have a text file, around 40,000 lines...
10
by: jojobar | last post by:
Hello, I am trying to use vs.net 2005 to migrate a project originally in vs.net 2003. I started with creation of a "web site", and then created folders for each component of the site. I read...
10
by: Rider | last post by:
Hi, simple(?) question about asp.net configuration.. I've installed ASP.NET 2.0 QuickStart Sample successfully. But, When I'm first start application the follow message shown. ========= Server...
53
by: Jeff | last post by:
In the function below, can size ever be 0 (zero)? char *clc_strdup(const char * CLC_RESTRICT s) { size_t size; char *p; clc_assert_not_null(clc_strdup, s); size = strlen(s) + 1;
13
by: Eric_Dexter | last post by:
All I am after realy is to change this reline = re.line.split('instr', '/d$') into something that grabs any line with instr in it take all the numbers and then grab any comment that may or may...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.