比如游戏运行过程中产生的信息导致原文件需要更改信息
1 #include<stdio.h>
2 #include<fcntl.h>
3 #include<unistd.h>
4 #include<stdlib.h>
5 #include<string.h>
6
7 int main(int argc,char **argv)
8 {
9 if(argc!=2){
10 printf("param error\n");
11 exit(-1);
12 }
13 printf("totall num of param is =%d\n",argc);
14 printf("num 1 param is %s\n",argv[0]);
15 printf("num 2 param is %s\n",argv[1]);
16
17 int fdsrc;
18 fdsrc = open(argv[1],O_RDWR);
19 if(fdsrc == -1){
20 printf("open file failed\n");
21 }
22
23 int size;
24 size=lseek(fdsrc,0,SEEK_END);
25 lseek(fdsrc,0,SEEK_SET);
26
27 char *buf=NULL;
28 buf = (char*)malloc(sizeof(char)*size+1);
29 if(buf == NULL){
30 printf("malloc memary failed\n");
31 }
32
33 int n_read = read(fdsrc,buf,size);
34 if(n_read == -1){
35 printf("read failed\n");
36 }
37
38 char *point = strstr(buf,"SPEED=");
39 if(point == NULL){
40 printf("strstr failed\n");
41 }
42 point = point+strlen("SPEED=");
43 *point = '9'; //一个字符可以,但两个以上就出现警告了,并且乱码
44 // strcpy(point,"100");
45
46 lseek(fdsrc,0,SEEK_SET);
47 int n_write = write(fdsrc,buf,strlen(buf));
48 if(n_write == -1){
49 printf("write failed\n");
50 }
51
52 printf("read %d byte to buf\n,write %d byte to fddes file\n",n_read,n_write);
53
54 close(fdsrc);
55
56 return 0;
57 }
strcpy()需要加强,原理没有弄清楚