473,398 Members | 2,343 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,398 software developers and data experts.

appending to a file

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::out|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
Jul 25 '08 #1
7 1817
mohi wrote:
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::out|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.
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 25 '08 #2
On Jul 25, 8:34 pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:
mohi wrote:
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::out|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.

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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
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....
Jul 25 '08 #3
mohi wrote:
On Jul 25, 8:34 pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:
>mohi wrote:
>>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::out|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.
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

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

Two things: don't quote signatures, and don't repeat what you already
told us.
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,
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?
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....
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 25 '08 #4
On Jul 25, 10:08 pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:
mohi wrote:
On Jul 25, 8:34 pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:
mohi wrote:
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::out|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.
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
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

Two things: don't quote signatures, and don't repeat what you already
told us.
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,

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?
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....

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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
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)
Jul 25 '08 #5
mohi wrote:
[..]
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. If
you have a C++ language question, go ahead and ask it. Other questions
belong to other newsgroups.

If I were to peek inside a binary file, I'd use my editor that allows me
to open non-text files and to display the information in hex format.

Good luck!

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 25 '08 #6
On Jul 25, 5:25 pm, mohi <mohangupt...@gmail.comwrote:
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::out|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.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jul 25 '08 #7
On Jul 25, 7:57 pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:
mohi wrote:
[..]
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.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jul 25 '08 #8

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

Similar topics

1
by: Thomas Heller | last post by:
I want to append/insert additional data to an xml file. Context: I use gccxml to parse C header files. gccxml creates an xml file containing all the definitions from the header files. The xml...
1
by: dmiller23462 | last post by:
Hey guys.... I put an error-handling in my page and have it posted at the complete end of the code, see below(when people were putting in 's I was getting the delimiter errors). Great, I...
7
by: Don | last post by:
Hi all, With regards to the following, how do I append the datetimestamp to the filenames in the form? The files are processed using the PHP script that follows below. Thanks in advance,...
1
by: Jonathan Taylor | last post by:
I have a large XML file, that is too large to read in to XmlDocument. I need to append data to this XML file without creating a new file, since I don't want to have two copies of the large file...
16
by: Michael | last post by:
I have a data application in a2k that I need to create two fixed width text files and then combine them to a single file The first file is header information and the second is transaction data. ...
2
by: tony.collings | last post by:
I started a thread here : http://groups.google.co.uk/group/microsoft.public.cmserver.general/browse_thread/thread/29d63077144004a9/c3888efdcb7338f6?hl=en#c3888efdcb7338f6 About creating an Email...
4
by: jasper | last post by:
How can this be done? Thanks
1
by: Frank | last post by:
Hi, Let's say I have a file named myFile.xml Within that file I have blocks of data which I'd like to add at different times during the day. e.g. <LogEntry>
2
by: sarada purkait | last post by:
hii i have to write into a file from the start and then go on appending to it .. i tried using ( ios::out|ios::app) but by this the file keeps on appending every time i run the program and the...
1
by: ofuuzo1 | last post by:
Hi, Is there anyway I can append a new element to an existing xml without first loading the existing file into a variable, adding the new element into the variable and saving it by overwriting the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.