Deleta Data in a file ?????????

This is a discussion on Deleta Data in a file ????????? within the C Programming forums, part of the General Programming Boards category; How do I seek and delete in a file ? I need to find a number in a file, and ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    64

    Delete Data in a file ?????????

    How do I seek and delete in a file ?

    I need to find a number in a file, and delete it.

    seekfile = fopen("seeknr.dat","a+");

    What shall I use to open the file with.
    a+ is only to append some data....
    w+ will overwrite the data in the file, when it opens it.
    r+ will only read.

    I need to both read, write and delete...

    Any good ideas...

    Hope to get help

    Gugge
    Last edited by Gugge; 07-04-2002 at 04:48 AM.
    !G!

  2. #2
    Registered User dune911's Avatar
    Join Date
    Sep 2001
    Posts
    140
    isn't write also read and delete ?

    i think only read is not able to write...
    there is no extra delete function afaik

  3. #3
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    >isn't write also read and delete ?
    No. "w" is ONLY to write data. Delete? Never.

    >i think only read is not able to write...
    Right. With "r" mode, you can only read data.

    >there is no extra delete function afaik
    Right.

    Gugge, the best bet for you would be to:
    1.) Create another temporary file ("w" mode)
    2.) Write in all the data you want (read from the original file)
    3.) Ignore all that you don't want
    4.) Remove the original file
    5.) Rename this temporary filename to the original filename

    Search the forums, you'll get many more ideas on this.

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    15
    [QUOTE]Originally posted by shaik786
    [B]>isn't write also read and delete ?

    No. "w" is ONLY to write data. Delete? Never.



    Requesting a clarifiecation , oh wise shaik.....

    Doesn't "w" overwrite a file if it exists...
    so you could skip the step of removing the original file by just overwriting it with the new data? (this may not be a good idea for other reasons)

    if so....
    maybe that's what the earlier post meant by "w" being delete

    if not....
    I need to throw away some of these damn books

    thanks,

    Regis

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >No. "w" is ONLY to write data. Delete? Never.
    That's "a", "w" will remove all previous data in the file before writing if possible, "a" leaves everything intact and sets the file pointer to the end of the file.

    >a+ is only to append some data....
    >w+ will overwrite the data in the file, when it opens it.
    >r+ will only read.
    a+ will append to the file, but you can also read.
    w+ will set the file to zero length if possible and you can also read
    r+ will both read and write, the file pointer is set to offset 0 and all data remains intact.

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

  6. #6
    Registered User raimo's Avatar
    Join Date
    Jun 2002
    Posts
    107
    1.) Create another temporary file ("w" mode)
    2.) Write in all the data you want (read from the original file)
    3.) Ignore all that you don't want
    4.) Remove the original file
    5.) Rename this temporary filename to the original filename
    How are you going to do this system-independently?
    Why not just read the file in buffer, edit buffer, and write the buffer to disk.

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. Replies: 48
    Last Post: 09-26-2008, 04:45 AM
  3. Bitmasking Problem
    By mike_g in forum C++ Programming
    Replies: 13
    Last Post: 11-08-2007, 12:24 AM
  4. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 04:44 PM
  5. File Database & Data Structure :: C++
    By kuphryn in forum C++ Programming
    Replies: 0
    Last Post: 02-24-2002, 11:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21