Thread: question about file handling ?!

  1. #1
    Registered User
    Join Date
    Oct 2015
    Posts
    11

    question about file handling ?!

    when you want to delete line from file or even edit it or add a line
    to do that i found algorithm or in fact two algorithms.

    first: when you want to delete or edit a line in a file called file.text(for example) you can copy all the contents of this file into another file called demo.text(for example) and when you reach the line number which you to delete it or to edit you can do that.

    the second algorithm is:
    to edit a line in a file called file.text you can copy all the contents of this file into temporary file and making the change after that remove file.text file then create a new file called file.txt after that copy the contents of temporary into another file called file.text
    and of course the temporary file automatically will be delete it.

    is there another algorithm to did it ?

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    One method is to read the file into memory, make your changes, then write it out to a new file. Then just delete the original file and rename the new file. For anything other than a basic append, this is much easier than trying to modify a file directly.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If your file is actually a simple (or even not so simple) relational database, you could consider using a self-contained serverless relational database engine like SQLite. Essentially, SQLite and its rivals compete with fopen to replace your own database file format. Of course, if the format that you have in mind is not well suited for a relational database, or if you are storing say, a text file for a story or something like that, then SQLite would not necessarily be the best tool for the job, so you might choose another tool, or even just stick with fopen.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    My recommended method would be for file.txt:

    1) Rename file.txt to file.bak (Or whatever backup file name)
    2) Open file.bak for reading, and file.txt for creation and writing.
    3) Read each line from file.bak and write to file.txt, EXCEPT for the line(s) to be removed.
    4) Close both files before exiting the program.

    This way you have a backup file preserved in case the program crashes or some other error occurred.

    EDIT: Check each step for errors as you process the file, including all reads, writes, and the rename!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Handling question
    By Sándor Csetneki in forum C Programming
    Replies: 2
    Last Post: 07-13-2014, 10:12 PM
  2. File Handling -Read write and edit a file
    By aprop in forum C Programming
    Replies: 3
    Last Post: 02-27-2010, 02:01 PM
  3. File handling question...
    By Raigne in forum C++ Programming
    Replies: 7
    Last Post: 04-10-2008, 02:02 AM
  4. file handling
    By vaibhav in forum C++ Programming
    Replies: 6
    Last Post: 12-23-2005, 12:12 PM

Tags for this Thread