Thread: Hypothetical question about deleting from a file

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    32

    Hypothetical question about deleting from a file

    to input information from a file or output for that matter, the program uses a pointer to determine location in a file. so if I create two arrays, one populated by the user, the other populated inputing from a file, part of my array input from file could be pointer location right?

    If so, with that in mind I should be able to use that information to delete information at that pointer location from the input file?

    basically I want to compare the user information and file input information and if they match delete the information from the file.

    I have no problem populating the arrays with the proper info, but I cant find away to delete the information at that point of the file.
    Anyone know of where I could research this? All my beginners books are not helping and I have not found anything on the net just yet, still looking though.....
    I'd like to put Murphy and his laws in headlock sometimes!

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Some choices:

    Load the file into memory, work with it there, then write the revised data back to disk, minus the deleted bits.

    Or, load the file a bit at a time, and write out to a new file with a different name, again minus the deleted bits. When you're done delete the original and rename the new file to the correct name.

    Never actually delete anything from the file, simply flag it some way that you application knows it's been deleted. This is a good option if you want to provide an undelete function, and you simply toggle the flag accordingly.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    > but I cant find away to delete the information at that point of the file.
    If your system supports record oriented files then you probably have operations to insert and delete with them, but typically you'll have to rewrite the entire file with the changes made. Or you could take the easy way out and simply mark the record as deleted and ignore it while reading.

    -Prelude
    My best code is written with the delete key.

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    It sounds like what you want to do is "erase" data in the middle of a file, which you can't do using standard I/O (you can fill it in with 0's or whatever if thats possible in your situation).
    It's similar to arrays - for example, if you have:
    Code:
    char a[5] = {'a','b','c','d','e'};
    and you want to "erase" the 'c', you have to move the 'd' and 'e' up one position in the array.
    If you have already loaded the entire file contents into an array, then you can take this approach using memmove(). You can then overwrite the file with the new data (don't forget to maintain the true length of the file data)

    Does this help?

    gg

  5. #5
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    The only way you can actually delete information is to over-write the entire file.... I guess Prelude already said that. Or, treating the file as binary, you could over-write the selected bytes with zeros (or some other value).

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    No default support for mmap() in Windows (maybe in Cygwin.dll), but there is support for memory mapped files under Win32.
    I think these are WAY above and beyond whats needed in sunburnbyRA's situation though

    gg

  7. #7
    Registered User
    Join Date
    Mar 2003
    Posts
    32
    Wow, alot of info.... Thanks everyone, sorry to just read, I was out of town this weekend, All that helps, I am using win2k for the os, I will load the old file into mem and then write to a new file and over write the old with the new.

    This will take a more cpu time and coding by the program, but probably the best way to handle the problem. But that answers my question as to whether or not there was another way to handle a file in that matter using array population and pointers.

    Thanks- to be continued.....
    I'd like to put Murphy and his laws in headlock sometimes!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  3. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  4. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM