removing data in files

Discussion in 'C Programming' started by blubzouf@gmail.com, Apr 18, 2006.

  1. Guest

    I am searching some info about accessing files with stdio functions.

    I am able to open a file, read in it with freaf, write in it with
    fwrite, modifying its data in "r+" mode ( without truncation nor
    appending ), but I have never found how to remove a piece of data
    inside a file, like having sequences "aaaa" "xxxx" "bbbb", and needing
    to removi second sequence to end with "aaaa" "bbbb", WITHOUT rewriting
    the whole file...

    Is that possible ? really..
    for example, how databases like sqlite do to delete rows so quickly in
    big files ? maybe they don't rewrite all the data, do they ?

    Somebody told me about "externals data structures" but I didn't found
    what he wanted to mean.
     
    , Apr 18, 2006
    #1
    1. Advertising

  2. Richard Bos Guest

    wrote:

    > I am able to open a file, read in it with freaf, write in it with
    > fwrite, modifying its data in "r+" mode ( without truncation nor
    > appending ), but I have never found how to remove a piece of data
    > inside a file, like having sequences "aaaa" "xxxx" "bbbb", and needing
    > to removi second sequence to end with "aaaa" "bbbb", WITHOUT rewriting
    > the whole file...


    Ah, ye antient and extincte art of faqqe-readinge...
    <http://c-faq.com/osdep/insdelrec.html>

    > for example, how databases like sqlite do to delete rows so quickly in
    > big files ? maybe they don't rewrite all the data, do they ?


    Nope. Generally, they just mark the row "deleted", and only really
    delete the marked rows in one great periodical purging operation, or
    they re-use marked rows.

    Richard
     
    Richard Bos, Apr 18, 2006
    #2
    1. Advertising

  3. <> wrote in message
    news:...
    > I am searching some info about accessing files with stdio functions.
    >
    > I am able to open a file, read in it with freaf, write in it with
    > fwrite, modifying its data in "r+" mode ( without truncation nor
    > appending ), but I have never found how to remove a piece of data
    > inside a file, like having sequences "aaaa" "xxxx" "bbbb", and needing
    > to removi second sequence to end with "aaaa" "bbbb", WITHOUT rewriting
    > the whole file...
    >
    > Is that possible ? really..


    No. At some point, the entire file has to be rewritten with the changes.

    > for example, how databases like sqlite do to delete rows so quickly in
    > big files ? maybe they don't rewrite all the data, do they ?
    >


    The answer to how quickly they do it is: linked-lists.

    Text editors and spreadsheets usually break the data up into smaller pieces,
    such as a "line" or "cell". These smaller pieces are then inserted into a
    linked-list of data structures, called "nodes", where one of the elements of
    the structure is a "line" or a "cell". Each node also contains other
    elements, such as pointers to the other data structures, to be able to
    create the linked-list. To delete a "line" or "cell", they delete the node
    from the linked list (by changing the pointers in two nodes to "skip" the
    deleted node). To delete text within a single "line" or "cell", they use
    functions like memcpy or memset to directly manipulate the contents of the
    "line" or "cell".


    Rod Pemberton
     
    Rod Pemberton, Apr 18, 2006
    #3
  4. Al Balmer Guest

    On Tue, 18 Apr 2006 16:13:33 -0400, "Rod Pemberton"
    <> wrote:

    >
    ><> wrote in message
    >news:...
    >> I am searching some info about accessing files with stdio functions.
    >>
    >> I am able to open a file, read in it with freaf, write in it with
    >> fwrite, modifying its data in "r+" mode ( without truncation nor
    >> appending ), but I have never found how to remove a piece of data
    >> inside a file, like having sequences "aaaa" "xxxx" "bbbb", and needing
    >> to removi second sequence to end with "aaaa" "bbbb", WITHOUT rewriting
    >> the whole file...
    >>
    >> Is that possible ? really..

    >
    >No. At some point, the entire file has to be rewritten with the changes.
    >
    >> for example, how databases like sqlite do to delete rows so quickly in
    >> big files ? maybe they don't rewrite all the data, do they ?
    >>

    >
    >The answer to how quickly they do it is: linked-lists.


    <OT> An even quicker method used by some databases is to simply mark
    the row "deleted". The slot may then be reused, or cleaned up at some
    later time. </OT>
    >
    >Text editors and spreadsheets usually break the data up into smaller pieces,
    >such as a "line" or "cell". These smaller pieces are then inserted into a
    >linked-list of data structures, called "nodes", where one of the elements of
    >the structure is a "line" or a "cell". Each node also contains other
    >elements, such as pointers to the other data structures, to be able to
    >create the linked-list. To delete a "line" or "cell", they delete the node
    >from the linked list (by changing the pointers in two nodes to "skip" the
    >deleted node). To delete text within a single "line" or "cell", they use
    >functions like memcpy or memset to directly manipulate the contents of the
    >"line" or "cell".
    >
    >
    >Rod Pemberton
    >


    --
    Al Balmer
    Sun City, AZ
     
    Al Balmer, Apr 18, 2006
    #4
    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. ion
    Replies:
    2
    Views:
    450
  2. Bhanu
    Replies:
    2
    Views:
    1,351
    Roedy Green
    May 5, 2006
  3. Anthony

    removing files

    Anthony, Jul 11, 2003, in forum: C Programming
    Replies:
    4
    Views:
    558
    Jack Klein
    Jul 12, 2003
  4. rbt
    Replies:
    5
    Views:
    8,992
  5. Chris  Chiasson
    Replies:
    6
    Views:
    635
    Richard Tobin
    Nov 14, 2006
Loading...

Share This Page