修改配置文件

比如游戏运行过程中产生的信息导致原文件需要更改信息

  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()需要加强,原理没有弄清楚

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值