Insert data to beginning of file instead of end??

Hi,
Everything I have read from searching online shows how to append to the end of a file using fstream and ios:app. I want to do the opposite, is it possible to insert data into the beginning of a file without overwriting the current content of a file?

Thanks!
This is quite hard. The following program will insert the line given into the beginning of data.txt.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "stdio.h"
#include "stdlib.h"
int main(){
	FILE *f=fopen("data.txt","rb");
	int len;
	void *data=NULL;
	if(f){
		fseek(f,0,SEEK_END);
		len=ftell(f);
		fseek(f,0,SEEK_SET);
		data=malloc(len);
		fread(data,1,len,f);
		f=freopen("data.txt","wb",f);
	}else f=fopen("data.txt","wb");
	char line[100];
	fgets(line,100,stdin);
	fputs(line,f);
	if(data){
		fwrite(data,1,len,f);
		free(data);
	}
	fclose(f);
}
I don't think I would try using that with large files.

Personally, I would write the data you want at the beginning to a new file, then the data of the old file after it--delete the old then rename the new to the old file. But that may be a bit of a sloppy hack.

If you're creative I bet you can read a block of the file's data then over-write a portion of it with the data you want at the beginning, then continue reading and writing through until the eof to preserve the file's data, basically swapping a portion to memory.

But, on the other hand I don't usually work with files like that.
Wow! thanks for the fast replys, let me give a little background information to help out.

I prefer to create the file in the correct order in one sweep but unfortunately I can't in this situation. I am dumping a undetermined amount of data from hardware to a file, this data can be upwards of a gigabyte; the amount of data I am going to capture is not know at the beginning. I then have to insert a header into the beginning of the file that contains data describing the file with the length of the file and other fields that do not have set lengths so I can not just create a place holding space for the data to be overwritten in later.


rocketboy9000, sorry if I am misunderstanding the code but it looks like it dumps the contents of the file to memory, I should have specified in the beginning that the file size I am working with may be too big to do that, opps.

taylorc8, I was trying to avoid the kludge of creating a new then appending the contents of the original file to the new file then deleting the original file but I guess it is unavoidable.


What is the fastest file operation to dump content from one file to another, I am using fstream .get() and .put() which I am sure is not great.

Thanks again for all the help!

It's fastest to call another program to do that for you:
1
2
string s="cp "+filename+" tempfile";
system(s);
[...] other fields that do not have set lengths so I can not just create a place holding space for the data to be overwritten in later.
Could you be more specific about the info that you want to store?
Maybe you could reserve more space than needed (fixed) and use a mark to say "here ends the header". Or work with binary data.

Is it really necessary for the header to be in the same file?
Topic archived. No new replies allowed.