配置文件的修改
-
之前配置好的一个文件test.config,内容是
-
现在如何把leng=6中的6 换个数字呢比如9,此时就需要用到一个字符串函数strstr(),他能找到我们需要操作的字符串。
首先我们先要找到位置a,然后a往后移几位到数字6(b),最后进行修改
代码实现
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
int main(int argc,char **argv)
{
int fd;
int n_read;
int n_write;
char *readBuf;
if(argc != 2){
printf("程序出错\n");
exit(-1);
}
fd=open(argv[1],O_RDWR);
int size=lseek(fd,0,SEEK_END);//定义一个size文件的大小
lseek(fd,0,SEEK_SET);//把光标移到开头
readBuf=(char *)malloc(sizeof(char)*size+4);
n_read=read(fd,readBuf,size);
// char *strstr(const char *haystack, const char *needle);
char *p=strstr(readBuf,"leng=");//先找到需要该的字符串的最前面,他的返回值是相当于l的前面
if(p == NULL){
printf("没有找到\n");
exit(-1);
}
p=p+strlen("leng=");//位置往后移到需要改数字那个地方,文件中是数字6,
*p='9';//就是把6的值改称9,写到文件里面的都是字符
n_write=write(fd,readBuf,n_read);
close(fd);
return 0;
}
结果
出了点小问题,还是光标的问题,应该在写入之前把光标移到最前面(或者代开文件的时候使用O_TRUNC)
改过之后的代码
加光标
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
int main(int argc,char **argv)
{
int fd;
int n_read;
int n_write;
char *readBuf;
if(argc != 2){
printf("程序出错\n");
exit(-1);
}
fd=open(argv[1],O_RDWR);
int size=lseek(fd,0,SEEK_END);//定义一个size文件的大小
lseek(fd,0,SEEK_SET);//把光标移到开头
readBuf=(char *)malloc(sizeof(char)*size+4);
n_read=read(fd,readBuf,size);
// char *strstr(const char *haystack, const char *needle);
char *p=strstr(readBuf,"leng=");//先找到需要该的字符串的最前面,他的返回值是相当于l的前面
if(p == NULL){
printf("没有找到\n");
exit(-1);
}
p=p+strlen("leng=");//位置往后移到需要改数字那个地方,文件中是数字6,
*p='9';//就是把6的值改称9,写到文件里面的都是字符
lseek(fd,0,SEEK_SET);//把光标移到开头
n_write=write(fd,readBuf,n_read);
close(fd);
return 0;
}
加O_TRUNC
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
int main(int argc,char **argv)
{
int fd;
int n_read;
int n_write;
char *readBuf;
if(argc != 2){
printf("程序出错\n");
exit(-1);
}
fd=open(argv[1],O_RDWR);
int size=lseek(fd,0,SEEK_END);//定义一个size文件的大小
lseek(fd,0,SEEK_SET);//把光标移到开头
readBuf=(char *)malloc(sizeof(char)*size+4);
n_read=read(fd,readBuf,size);
// char *strstr(const char *haystack, const char *needle);
char *p=strstr(readBuf,"leng=");//先找到需要该的字符串的最前面,他的返回值是相当于l的前面
if(p == NULL){
printf("没有找到\n");
exit(-1);
}
p=p+strlen("leng=");//位置往后移到需要改数字那个地方,文件中是数字6,
*p='9';//就是把6的值改称9,写到文件里面的都是字符
close(fd);
open(argv[1],O_RDWR|O_TRUNC);
//lseek(fd,0,SEEK_SET);//把光标移到开头
n_write=write(fd,readBuf,n_read);
close(fd);
return 0;
}
运行结果和上图一样。
补充:将结构体写入文件
废话不多说。直接撸代码
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
struct test
{
int a;
char c;
};
int main()
{
int fd;
int n_read;
int n_write;
struct test data1={100,'a'};
struct test data2;
fd=open("./file13",O_RDWR);
n_write=write(fd,&data1,sizeof(struct test));//将data1里的内容写到fd
lseek(fd,0,SEEK_SET);//把光标移到最前面
n_read=read(fd,&data2,sizeof(struct test));//将内容独到data2里面
printf("read :%d,%c\n",data2.a,data2.c);
close(fd);
}