2021-10-14 Linux系统编程10.文件编程小应用之【修改程序的配置文件】

本文介绍如何使用C语言通过main参数和字符串操作,动态修改配置文件中的特定内容。通过open(), read(), write(), lseek()等函数实现文件的读取、定位和内容替换,适合初学者理解文件操作基本原理。
摘要由CSDN通过智能技术生成

10.文件编程小应用之【修改程序的配置文件】

转载:沈上青灯
编程思路:
利用main参数(main参数的简解)
打开要修改的文件
将要修改文件的内容读到 buf
利用字符串API之strstr( ) 找到要修改的子串的位置,修改内容
将修改后的buf写回源文件
close关闭文件

#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;//file description
        char *readBuf=NULL;
        if(argc != 2){
                printf("pararm error\n");
                exit(-1);
        }
        fdSrc=open(argv[1],O_RDWR);//以参数的方式传递配置文件./a.out TEST.config
        int size = lseek(fdSrc,0,SEEK_END);//利用lseek返回值计算文件的大小

        readBuf = (char *)malloc(sizeof(char)*size + 8);

        lseek(fdSrc,0,SEEK_SET);
        int n_read = read(fdSrc,readBuf,size);//2.将要修改文件的内容读到 readbuf

        char *p=strstr(readBuf,"LEND=");//3.利用字符串API之strstr( ) 找到要修改的子串的                                         位置,返回的是子串的 L 的地址
        if(p==NULL){
                printf("not found\n");
                exit(-1);
        }
        p = p+strlen("LEND=");//指针偏移到 = 号后一位,但是写进文件的时候必须是字符
        strncpy(p,"5",3);// *p = '5';

        lseek(fdSrc,0,SEEK_SET);//防止文件从光标后面添加
        int n_write = write(fdSrc,readBuf,size);//4.将修改后的buf写回源文件
        close(fdSrc);
        return 0;
}

```或者通过open直接打开文件

#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>

int main()
{
        int fdSrc;//file description
        char *readBuf=NULL;
        fdSrc=open("TEST.config",O_RDWR);//以参数的方式传递配置文件./a.out TEST.config
        int size = lseek(fdSrc,0,SEEK_END);//利用lseek返回值计算文件的大小

        readBuf = (char *)malloc(sizeof(char)*size + 8);

        lseek(fdSrc,0,SEEK_SET);
        int n_read = read(fdSrc,readBuf,size);//2.将要修改文件的内容读到 readbuf

        char *p=strstr(readBuf,"LEND=");//3.利用字符串API之strstr( ) 找到要修改的子串的                                         位置,返回的是子串的 L 的地址
        if(p==NULL){
                        printf("not found\n");
                exit(-1);
        }
        p = p+strlen("LEND=");//指针偏移到 = 号后一位,但是写进文件的时候必须是字符
        strncpy(p,"101",3);

        lseek(fdSrc,0,SEEK_SET);//防止文件从光标后面添加
        int n_write = write(fdSrc,readBuf,size);//4.将修改后的buf写回源文件

        close(fdSrc);
        return 0;
}
## 2.字符串的拷贝API:strncpy()
```c
	原型:char *strncpy(char *destinin, char *source, int maxlen);
	- 第一个参数dest:目标
 	- 第二个参数src:源
 	- 第三个参数src:最大长度,就是要拷贝几个

	memset(strDest,'\0',128);  	初始化,重置数组。这里用sizeof(strDest)/sizeof(strDest[0])结果会出错(细节)
    strncpy(strDest,"TheLuk",3);
    puts(strDest);	
    - 输出的结果是:The

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值