changenum.c 1.12 KB
/*
 * read in a file, change the first word which tells how many 
 * data structures are in the file, and rewrite out
 */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <netinet/in.h>


  
     
int main(int argc, char **argv){
   
  int numread;
  FILE *infileptr;
  FILE *outfileptr;
  unsigned char tempbuf[256];
  unsigned long outnumber;
  int blocknum = 0;

  if(argc != 4){
    fprintf(stderr,"usage: %s  inputfile outputfile newnumber\n", argv[0]);
    exit(1);
  }
  
    
  /*open files */
  infileptr = fopen(argv[1], "r");
  outfileptr = fopen(argv[2], "w");
  if((infileptr == NULL) ||(outfileptr == NULL)){
    fprintf(stderr,"couldnt open files\n");
    exit(1);
  }
  outnumber = htonl((unsigned long) atoi(argv[3]));
  do{
    numread = fread(tempbuf, 1, sizeof(tempbuf), infileptr);
    /* change the first word */
    if(blocknum == 0){
      memcpy(tempbuf, &outnumber, sizeof(unsigned long));
    }
    fwrite(tempbuf, 1, numread, outfileptr);
    blocknum++;
  } while(numread == sizeof(tempbuf));

  fclose(infileptr);
  fclose(outfileptr);

  return 0;
}