Thread: File appending

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

    File appending

    Hello!

    I had a question about appending data in to a file.

    My program flows in the following way--
    Code:
    // This function reads data from the file
    parse_input_data_for_one_algo(some parameters) 
    {
    FILE *f = fopen (filename, "rw+");
    while ( fgets (in_line, 100, f) != NULL )
    {
    //read values
    }
    fclose(f);
    }
    Code:
    parse_input_data_for_second_algo(some parameters) 
    {
    FILE *f = fopen (filename, "rw+");
    while ( fgets (in_line, 100, f) != NULL )
    {
    //read values
    }
    fclose(f);
    }
    Code:
    // Call to both parse functions in another file which contains the main program
    int main(int argc, char **argv)
    {
    char *filename = argv [1];
    parse_input_data_for_one_algo (filename,some parameters);
    //Algorithm-1
    char *filename1 = argv [1];
    parse_input_data_for_second_algo (filename1,some parameters);
    //Algorithm-2
    }
    My text file is in the following format--

    x 12 44
    y 3 1
    y 1 2 -1
    z 1 2 1
    z 3 1 2
    z 4 1 3
    z 6 1 4
    t 1 2 1 2 1
    t 1 2 3 1 2
    t 1 3 4 1 1
    I am trying to write additional data from the main file after "Algorithm-1" is solved, in to the text file after all the "z" s

    So my new text file with appended data would look like this --
    (1,2,6,9 have been appended to the file)
    x 12 44
    y 3 1
    y 1 2 -1
    z 1 2 1 1
    z 3 1 2 2
    z 4 1 3 6
    z 6 1 4 9
    t 1 2 1 2 1
    t 1 2 3 1 2
    t 1 3 4 1 1
    The new appended text file would now be read by parse_input_data_for_second_algo.
    Kindly let me know of alternatives of how I could solve this problem.

    Thanks in advance!

  2. #2
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    AFAIK, you're stuck with basically writing a new file, deleting the old one and renaming the new one to match the old filename

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Read data from text file into a buffer; filter line that contains "z"; append main file data into buffer that holds the "z" line.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  3. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  4. 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