| 
	
 | 
 Posted by Jerry Stuckle on 07/31/07 01:23 
ttrium wrote: 
> Ok, so this is probably the easist question ever posted, but I 
> honestly can't find the answer (if there is one). 
>  
> How do I (by which function) add data to a file at the beginning of a 
> file, rather than the end of the file (as is with fopen(file, "a"))? 
>  
 
Two ways: 
 
1. Open the file, read it all into memory, truncate the file, write the  
new data followed by the old data. 
 
2. Open the file, open a temporary file, write the new data to the  
temporary file, copy the data from the original file to the temporary  
file, close both files. delete the original file and rename the  
temporary file. 
 
#2 is obviously more work, but it doesn't require lots of memory to read  
a large file into memory, and won't lose the file if the system crashes  
in the middle. 
 
--  
================== 
Remove the "x" from my email address 
Jerry Stuckle 
JDS Computer Training Corp. 
jstucklex@attglobal.net 
==================
 
[Back to original message] 
 |