c - How to delete first n characters from a binary file -


i have binary file want delete first n characters before further processing. have file pointer input.

i tried use ftruncate had create file pointer don't want. tried below code not help.

#include <stdio.h> #include <unistd.h>  int main(void) {          file*f,ftemp;         f=fopen("./temp","a");         scanf("%d",&n);         fseek(f,n,seek_set);         ftruncate(fileno(f),/*end of file*/ );         ftemp=f;         return 0; } 

please suggest other way.

actually first n bytes binary , rest of part asn.

thanks

here program lop off first n bytes of ./temp, leaving original file ./temp-old. assumes file fit in memory. specify n on command line.

/*  * program provided "as is" without warranty of 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) 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 not 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 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 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 carefully, but, of course should make backup copy of file before using it, in case...


Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -