Here is a program which will lop off the first N bytes of ./temp, leaving the original file as ./temp-old.
It assumes the file will fit in memory.
You specify N on the command line.
/*
* THIS PROGRAM IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
* EITHER EXPRESSED OR IMPLIED.
*/
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#define ERR (-1)
int main (
int argc,
char ** argv
) {
int fdin;
int fdout;
unsigned long n;
struct stat st;
size_t sz;
ssize_t ssz;
char * data;
if (argc != 2) {
fprintf(stderr, "usage: %s nbytes\n", argv[0]);
return 1;
}
n = strtoul(argv[1], NULL, 10);
if (errno) {
fprintf(stderr, "nbytes (%s) is suspect\n", argv[1]);
return 1;
}
fdin = open("./temp", O_RDONLY, 0);
if (fdin == ERR) {
fprintf(stderr, "open input: %s\n", strerror(errno));
return 1;
}
if (fstat(fdin, &st) == ERR) {
fprintf(stderr, "stat input: %s\n", strerror(errno));
return 1;
}
sz = st.st_size;
if (sz < n) {
fprintf(stderr, "file is not that big\n");
return 1;
}
data = malloc(sz);
if (data == NULL) {
fprintf(stderr, "insufficient memory\n");
return 1;
}
ssz = read(fdin, data, sz);
if (ssz < 0) {
fprintf(stderr, "read input: %s\n", strerror(errno));
return 1;
}
if ((size_t)ssz != sz) {
fprintf(stderr, "read was short\n");
return 1;
}
(void)close(fdin);
fdout = open("./temp-new", O_CREAT|O_EXCL|O_WRONLY, st.st_mode);
if (fdout == ERR) {
fprintf(stderr, "open output: %s\n", strerror(errno));
return 1;
}
sz -= n;
ssz = write(fdout, data + n, sz);
if (ssz < 0) {
fprintf(stderr, "write output: %s\n", strerror(errno));
return 1;
}
if ((size_t)ssz != sz) {
fprintf(stderr, "write was short\n");
return 1;
}
if (close(fdout) == ERR) {
fprintf(stderr, "write close: %s\n", strerror(errno));
return 1;
}
if (link("./temp", "./temp-old") == ERR) {
fprintf(stderr, "link input: %s\n", strerror(errno));
return 1;
}
if (rename("./temp-new", "./temp") == ERR) {
fprintf(stderr, "rename output: %s\n", strerror(errno));
return 1;
}
return 0;
}
I wrote it carefully, but, of course you should make a backup copy of your file before using it, just in case...
ncharacters, seeking back and forth, then truncate bynat the end of the process, which is all riskier. It's simpler and probably more efficient just to use a new file. You can't simply delete characters from the file.