changenum.c
1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
* 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;
}