Thread: Round-Robin File

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    1

    Post Round-Robin File

    Hi there!

    What, in everyone's opinion, would be the best way to iteratively remove the top line of a file, move all the other lines up to fill the empty space, and then add another line at the bottom.

    So there would be a file like

    Value1 Value2 <------ delete this line
    Value3 Value4
    Value5 Value6
    Value7 Value8
    Value9 Value10 ^move all lines up one line
    Value11 Value12
    Value13 Value14
    Value15 Value16
    Value17 Value18 <------ add "Value 19 Value 20" in here


    This way the size of the file remains fixed.

    Any thoughts?

    Thanks

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Create a temporary file. Write all but the first line of the original file to it, append the new entry, delete the original file, and then rename the temp file to the original file name.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Since the file requires rewriting anyway, you could open it, and open a new tmp file, read the first line, then start copying one line at a time until you reach the end, then add a line. Then delete the old file and rename the new one.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  4. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 08:19 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM

Tags for this Thread