Thread: Deleting content from a file

  1. #1
    Registered User Phoenix_Rebirth's Avatar
    Join Date
    Dec 2005
    Location
    Cyprus
    Posts
    68

    Deleting content from a file

    If I have a file containing some text data separated by some markers i.e '#####'
    how would I go about deleting some part lets say the third in order.

    If my file is like this
    #####
    first paragraph
    first line
    #####
    second paragraph
    second line
    *#####
    third paragraph
    third line
    *#####
    fourth paragraph
    fourth line
    #####
    I was thinking about reading lines using fgets(line, MAX, fd) along with a counter so I can go to the separator before the thrid paragraph and the one after it. (The marked ones with *)
    So I can get the positions in the file using ftell-strlen(line) when I am in the third and fourth occurrence of #####. Or I can use fseek when I am at the line I want and I just offset the negative length of len.
    My question is how can I delete the text in between the marks so I could get something like this.
    #####
    first paragraph
    first line
    #####
    second paragraph
    second line
    #####
    fourth paragraph
    fourth line
    #####
    I think I should open the file in r+ mode so I can update it but I don't know what else. Can I use write and write some special character?
    Last edited by Phoenix_Rebirth; 11-22-2010 at 05:58 PM.

  2. #2
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    Can I use write and write some special character?
    Nope, you have to read the file in and write everything but what you want to delete.

    Question 19.14
    Consider this post signed

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. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM