appending to a file

Discussion in 'C++' started by mohi, Jul 25, 2008.

  1. mohi

    mohi Guest

    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
     
    mohi, Jul 25, 2008
    #1
    1. Advertising

  2. mohi

    mohi Guest

    On Jul 25, 8:34 pm, Victor Bazarov <> wrote:
    > 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::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.

    >
    > 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....
     
    mohi, Jul 25, 2008
    #2
    1. Advertising

  3. mohi

    mohi Guest

    On Jul 25, 10:08 pm, Victor Bazarov <> wrote:
    > mohi wrote:
    > > On Jul 25, 8:34 pm, Victor Bazarov <> wrote:
    > >> 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::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.
    > >> 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)
     
    mohi, Jul 25, 2008
    #3
  4. mohi

    James Kanze Guest

    On Jul 25, 5:25 pm, mohi <> wrote:

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

    --
    James Kanze (GABI Software) email:
    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
     
    James Kanze, Jul 25, 2008
    #4
  5. mohi

    James Kanze Guest

    On Jul 25, 7:57 pm, Victor Bazarov <> wrote:
    > 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:
    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
     
    James Kanze, Jul 25, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. scopp
    Replies:
    3
    Views:
    434
    Guest
    Jan 22, 2004
  2. Marcelo
    Replies:
    5
    Views:
    10,288
    Gordon Beaton
    Oct 25, 2005
  3. Red Ogden
    Replies:
    0
    Views:
    1,100
    Red Ogden
    Jul 16, 2003
  4. Thomas Heller

    appending data to an xml file

    Thomas Heller, Jan 11, 2005, in forum: Python
    Replies:
    1
    Views:
    353
    Peter Hansen
    Jan 12, 2005
  5. Greg McIntyre
    Replies:
    0
    Views:
    435
    Greg McIntyre
    Sep 12, 2005
Loading...

Share This Page