I am trying to figure out how to write a new line of text at the beginning of a file (a header) . I know that I can open the file, or try to start a file, but if I write with it, then it will be written on it that is there. Do I have to write a new file and then write the second line by line, or is there a better way?
Example file:
1, 01 / 01/09, somedata, foo, bar 2, 01/02/09, somedata, foo, bar 3, 01 / 03/09, somedata, foo, bar
and I want to end
3, 1-3, 01/04/09 1, 01/01/09, somedata, foo, bar 2, 01/02/09, somedata, foo, bar3, 01/03/09, somedata, foo, bar
Edit:
This is what I am doing:
file * source; File * output; Four buffer [4096]; Size_t bytes read; ("Output", "w +"); Fprintf (output, "my header text", "afterfox", "federate (& buffer, 1, bytes read, output);} fprintf (output," my footer "); fclose (source); fclose Output); Remove (Source); Rename ("output.txt", "source.txt");
You will have to rewrite the whole file, but that does not mean that you have to read and write the original data line-by-line. Reading from the lines is relatively slow, so if the file fits in memory To be If it is too small for you, then read it in a bigger portion (or in a bigger block) and then after writing your new data, write it back.
Comments
Post a Comment