appending to a file

M

mohi

hello everyone,
i have a program where i write a structure of 1020B to a file a number
of times and in the next run of the program i have to again write
those structures but by taking some count from the last structure
written on the file
so i write it like:

fstream file("xyz",ios::eek:ut|ios::in|ios::ate);

long pos=file.tellg();

pos-=sizeof(struct);

file.putg(pos,0);//position at pos

file.read(reinterpret_cast<char *>(&struct),sizeof(struct));

use struct.cout;///////


the problem is that after the read struct has a count which is of the
second last struct written

can some one help me why this gets two structs back in the file.


thank you very much
 
M

mohi

How do you know that it is what's happening? Can you print the
position? Can you examine the file at that position? Can you tell
exactly how far from the end the file gets positioned? What's the file
size? Do you know with certainty that the "last struct" actually does
get written to the file?

Direct access database can be tricky. To debug it you need to be able
to look at the file at the byte level, at the record level, and
understand how your data get written (or even if they get written).

V

thanks for repling.....
this is actually an implementation of bst(binary search tree) written
on a file(with pointers as file position pointers)
now i display the structure after its written to the file and it has a
count field thats incremented for each write



now when run for the first time say the last output says that the
present count is 10(say)...that is a record with count=10 is written
to the file,
now in the next run where the file is opened as ios::ate ,and after
substracting one sizeof(strut) from the present file pointer
it reads a structure that has a count=9,though it shud have been 10.
now thats the problem....
 
M

mohi

Two things: don't quote signatures, and don't repeat what you already
told us.


Do you see it in the file? Do you have the tools (other than your own
code) which would read the file and allow you to inspect its content?


Your code consists of two large parts. First is *writing* to the file.
Make sure it works. When you know that writing works, you can debug the
*reading* of the file. If you don't know *for sure* that either is OK,
you cannot rely on one to debug the other.

Forget reading from the file for now. Find a tool that would allow you
to dump the contents of the file in, say, hexadecimal form, and examine
them carefully, making sure that what you say happens actually *does*
happen. Otherwise it's just your imagination at work.

Now, if you want some kind of theoretical advice on why your program
(which I've never seen) does what you think it does (that I have doubts
about anyway), then you have it: your reading does not work because your
writing does not work. If you want *concrete* help with your program,
*post your code*.

V

thanks for the reply ,but which tool can i use to see the contents of
the file and yes one more thing
which debuging the program with gdb it does follow the path lines as
written in the program

like for a single line say i++;
it goes on showing the same thing again and again without moving to
the next line(is it because after the compilation of the program
some ^M like characters have been added in the source file
automatically : i use g++ on cmd line)
 
J

James Kanze

i have a program where i write a structure of 1020B to a file
a number of times and in the next run of the program i have to
again write those structures but by taking some count from the
last structure written on the file so i write it like:
fstream file("xyz",ios::eek:ut|ios::in|ios::ate);
long pos=file.tellg();

What makes you think that the return type of tellg can be
implicitly converted to a long; it's certainly not guaranteed
by the standard. And what makes you thing that the result of
such a conversion has any numerical significance, even if it is
supported.
pos-=sizeof(struct);
file.putg(pos,0);//position at pos

You've lost me here. There is no iostream::putg function in my
copy of the standard. If you mean seekg, then the operation
you're looking for is
file.seekg( -sizeof(struct), ios::end ) ;
. Except that it isn't legal unless you've opened the file in
binary mode.
file.read(reinterpret_cast<char *>(&struct),sizeof(struct));

Which, of course, doesn't give anything useful, ever.
use struct.cout;///////
the problem is that after the read struct has a count which is
of the second last struct written
can some one help me why this gets two structs back in the
file.

Well, to start with, you'll have to write something that doesn't
use reinterpret_cast, if you expect it to work. And you'll have
to define the format of the data on the disk, so we can tell
what you're really doing. But if you've defined a fixed length
format, and open the file in binary mode, then seeking the
length of the format from the end of the file should work.
(Formally, "A binary stream need not meaningfully support fseek
calls with a whence value of SEEK_END", but you shouldn't run
into problems under Windows or Unix.)
 
J

James Kanze

mohi said:
[..]
thanks for the reply ,but which tool can i use to see the
contents of the file and yes one more thing which debuging
the program with gdb it does follow the path lines as
written in the program
like for a single line say i++;
it goes on showing the same thing again and again without
moving to the next line(is it because after the compilation
of the program some ^M like characters have been added in
the source file automatically : i use g++ on cmd line)
I have no idea what to tell you about the tools (whether to
examine the contents of the file or gdb). The tools are *not*
on topic here.

Although not really on topic: under Unix, od can be used for
dumping the file. If you've a Unix like tookkit under Windows,
you probably have it as well. On the other hand (and on topic),
if you're going to be fooling around with binary files, writing
such a program yourself in C++ would be a good exercise to begin
with.
If you have a C++ language question, go ahead and ask it.

And start by posting working code, showing exactly what you're
doing. (And as you say: first, how he writes the file, and then
how he tries to update it.)
 

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

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top