tags:

views:

85

answers:

4

How can I go back to the very beginning of a csv file and add rows?

(I'm printing to a CSV file from C using fprintf(). At the end of printing thousands of rows (5 columns) of data, I would like to go back to the top of the file and insert some dynamic header data (based on how things went printing everything). )

Thank You.

+5  A: 

Due to the way files are structured, this is more or less impossible. In order to accomplish what you want:

  1. write csv data to file1
  2. write header to file2
  3. copy contents of file1 to file2
  4. delete file1

Or you can hold the csv data in ram and write it to file after you're finished processing and know the header.

Another option is to set aside a certain number of bytes for the header, which will work much faster for large files at minimal space cost. Since the space is allocated in the file at the start of the write, there aren't any issues going back and filling it in. Reopen the file as random access ("r+"), which points to the top of the file by default, write header, and close.

Adam Shiemke
looks like 2 files is the only way to add dynamic header data, thank you.
Tommy
+1  A: 

I think that is not possible. Probably the easiest way would be to write the output to a temporary file, then create the data you need as the dynamic header, write them to the target file and append the previously created temporary file.

PeterK
creative, nice....
Tommy
+1  A: 
  • write enough blank spaces in the first line
  • write data
  • seek(0)
  • write header - last column will be padded with spaces
Marco Mariani
"write enough blank spaces in the first line" - is this assuming adding header all on one row?
Tommy
No, the actual number of bytes that your header will take up. This can span as many lines as you want.
Adam Shiemke
That's only useful if he knows exacly how many rows have to add..
Hernán Eche
+1  A: 

The simplest way would be to simply store the entire contents of the file in memory until you are finished, write out the header, and then write out the rest of the file.

If memory is an issue and you can't safely store the entire file in memory, or just don't want to, then you could write out the bulk of the CSV data to a temporary file, then when you are finished, write the header out to the primary file, and copy the data from the temporary file to the primary file in a loop.

If you wanted to be fancy, after writing the main CSV data out to the primary file, you could loop through the file from the beginning, read into memory the data that you're about to overwrite with the header, then write the header over top of that data, and so forth, read each chunk into memory, overwrite it with the previous one until you reach the end and append the final chunk. In this way you "insert" data at the beginning, my moving the rest of the file down. I really wouldn't recommend this as it will mostly just add complexity without much benefit, unless there is a specific reason you can't do something simpler like using a temporary file.

Brook Miles