修改文件
突破口:
char *p = strstr(readBuf,“LENG=”);//查找"LENG=“进行修改
p = p + strlen(“LENG=”);//指针移动到要修改的位置
//注意一定要’'而不是”"不然会出现错误
*p = ‘5’; //修改内容
可以对其进行封装成函数
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<stdlib.h>
int main( int argc, char **argv){
//传入一个文件
int fdSrc;
char *readBuf = NULL;
fdSrc = open(argv[1],O_RDWR);
int size = lseek(fdSrc,0,SEEK_END);
lseek(fdSrc,0,SEEK_SET);
readBuf = (char *)malloc(sizeof(char)*size+8);
read(fdSrc,readBuf,size);
char *p = strstr(readBuf,"LENG=");//查找"LENG="进行修改
p = p + strlen("LENG=");//指针移动到要修改的位置
//注意一定要''而不是""不然会出现错误
*p = '5'; //修改内容
lseek(fdSrc,0,SEEK_SET);//移动光标到头
//重新写入
int n_write = write(fdSrc,readBuf,strlen(readBuf));
close(fdSrc);
return 0;
}