How do you truncate a file?

Rudi Cilibrasi cilibrar at athena.ecs.csus.edu
Sun May 5 12:43:48 AEST 1991


Suppose I have a 100-byte file called "myfile.dat", and I want to get
rid of the last 10 bytes.  The only way that I can figure out how to do
this is with something like the following:

main()
{
	char buf[100];
	FILE *fp;
	fp = fopen("myfile.dat", "r");
	fread(buf, 1, 100, fp);
	fclose(fp);
	fp = fopen("myfile.dat", "w");
	fwrite(buf, 1, 90, fp);
	fclose(fp);
}

This seems to be a rather roundabout way to accomplish a simple task.
Is there any way to accomplish the same thing without reading the entire
file into memory?  Thanks for any help.



More information about the Comp.lang.c mailing list