文件编程小应用之修改配置文件内容(linux系统编程)

配置文件的修改

  • 之前配置好的一个文件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);
}

在这里插入图片描述

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值