Thread: How to write a program to delete the content in txt .........

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    2

    How to write a program to delete the content in txt .........

    Let say inside txt file got :
    ID: 12345
    Name: Kelvin

    and i just key in 12345 then can delete the user name Kelvin ...

    What is the code for doing it ?

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    If there's multiple entries like this in the file, you can't just delete arbitrary sections of the file. You need to read them into a data structure, remove what you want from the structure, and then write the structure back out to the file. If you have no idea where to start, learn about structs, linked lists, and then you should know all the basics to get this done.

    edit: I see your original thread, and the answer is pretty much the same: The file is just for permanent storage - when you're manipulating data, you really need to have it in some kind of data structure. Also, this board has a strict No Homework policy. You're welcome to ask for help with homework questions, but when you do so you must show your attempt and ask specific questions.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Location
    Rochester, NY
    Posts
    196
    Best bet is to:

    - Cache the file as a double pointer (char**)/2D char array.

    - Scan the cached copy line by line until you find a match, when you do, null that line and the next.

    - Shift the array down 2 to collapse it, or if you don't care about white space don't.

    - Write it back out to a file.

    That's kind of a quick and dirty method, though. Serializing the data in the first place would probably be better, as would loading up a data structure as sean mentioned.

    PS:
    This type of work is crappy (although completely do-able, of course) for C. If you want heavy string editing in a file and/or file manip use Ruby, PHP, Python, or Perl - pretty much any scripting language, I suppose.

    Each language has a useful domain. C's is close to the hardware and extremely efficient.

    Efficiency doesn't matter much in this situation, especially since it's a situation that's dependent on blocking for user input.

    Use a scripting language, it'll be like 20 lines if that, and it'll be easy as hell, lol.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 03-17-2010, 11:18 PM
  2. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  3. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  4. memory management...
    By master5001 in forum Game Programming
    Replies: 24
    Last Post: 01-07-2002, 05:50 PM