Thread: Deleting part of a file

  1. #1
    Registered User Invincible's Avatar
    Join Date
    Feb 2002
    Posts
    210

    Deleting part of a file

    Does anyone know how to delete a record from a file? I had an algorithm that would take the next record in the file and write it over the previous record and so on until there were 2 like records at the end of the file, but how would one go about completely removing the bytes of one record from a file? I thought maybe truncate, but that resets the entire file to 0bytes right?
    "The mind, like a parachute, only functions when open."

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    The way I solved a similar program where I had abstract data in a custom linked list was using copy, remove, and write.

    - read data from file into a data structure
    - remove whatever data you want
    - remove(filename) // delete the data file
    - write new data file with the updated data structure

    Kuphryn

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Yep, that's the only way...'cept another way to 'erase' the file, without calling remove is:

    FILE *f = fopen(bFile, "r+");

    //..read in, delete entries - then:

    int handle = fileno( f );

    chsize( handle );
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    Registered User Invincible's Avatar
    Join Date
    Feb 2002
    Posts
    210
    Well, Sebastiani, that's C, but I suppose I could open the file with ios_base::trunc and truncate it to 0bytes effectively deleting the entire contents of the file. Then I could rewrite the data minus the record I want to delete. That seems like an enourmous load just to delete one record though...

    Any other thoughts on deleting just one part of a file?

    Thanks for you suggestions, Invincible
    Last edited by Invincible; 05-23-2002 at 12:57 AM.
    "The mind, like a parachute, only functions when open."

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    There is no other way.

    What's so hard, anyway?

    BTW: I am a halfling. Half C, half C++.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    Registered User Invincible's Avatar
    Join Date
    Feb 2002
    Posts
    210
    Ahh, it's not that it's overly difficult to code, but that it seems like a work-around for what should be a standard library function. That's all.

    Friends don't let friends mix C and C++

    Halfling ... bah ... I am an ogre. I eat halflings

    Edit:

    Just think of it this way...

    to delete one file, you're allocating (record[bytesOfMemory] * numberOfRecords) just to remove one record from a file. Depending on the number and size of the records, that could be a very inefficient way of doing things.
    Last edited by Invincible; 05-23-2002 at 02:17 AM.
    "The mind, like a parachute, only functions when open."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM