自学内容网 自学内容网

C语言更改Linux文件的配置内容

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
int main(int argc,char **argv) 
{
int fdSrc;
if (argc != 3)
{
printf("pararm error\n");
exit(-1);
}
fdSrc = open(argv[1],O_RDWR);
char *readBuf;
lseek(fdSrc,0,SEEK_SET);
int n_write = lseek(fdSrc,0,SEEK_END);
lseek(fdSrc,0,SEEK_SET);
printf("SrcFileSize = %d\n",n_write);
readBuf = (char*)malloc(sizeof(char)*n_write);
read(fdSrc,readBuf,n_write);
lseek(fdSrc,0,SEEK_SET);//读完光标要回到开始这样子才写了才会覆盖
char *p = strstr(readBuf,"LENG=");//找到存在字符串LENG=的地方,返回开头地址
if (p==NULL)
{
printf("no found\n");
exit(-1);
}
p = p + strlen("LENG=");
*p = argv[2][0];
write(fdSrc,readBuf,n_write);
close(fdSrc);
return 0;
} 

 


原文地址:https://blog.csdn.net/cykaw2590/article/details/144096899

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!