Linux文件I/O修改程序的配置文件(文件编程总结三)

文件编程总结三

项目名称:Linux文件I/O修改程序的配置文件

目的:进一步锻炼linux文件I/O编程基本函数的使用:open 、read、write、close、lseek;

在项目开始之前我们再引入LinuxC下的字符串strstr函数

函数功能:从字符串haystack中寻找needle第一次出现的位置,但是该函数不比较结束符NULL。
  
返回说明:返回指向第一次出现needle位置的指针,如果没找到则返回NULL。

SYNOPSIS
       #include <string.h>

       char *strstr(const char *haystack, const char *needle);//参数说明:haystack为一个源字符串的指针,needle为一个目的字符串的指针。
        

       #define _GNU_SOURCE         /* See feature_test_macros(7) */

       #include <string.h>

       char *strcasestr(const char *haystack, const char *needle);
/*
DESCRIPTION
       The  strstr() function finds the first occurrence of the substring nee‐
       dle in the string haystack.  The terminating null bytes ('\0') are  not
       compared.

       The  strcasestr()  function  is  like strstr(), but ignores the case of
       both arguments.

RETURN VALUE
       These functions return a pointer to the beginning of the substring,  or
       NULL if the substring is not found.
*/

该函数的编程图解:


       

代码的初步实现:

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

int main(int argc , char *argv[])
{
        int fd;

        char *readBuf = NULL;

        if(argc != 2){
                printf("param error!\n");
                exit(-1);

        }

        fd = open(argv[1],O_RDWR);

        int size = lseek(fd,0,SEEK_END);
        lseek(fd,0,SEEK_SET);

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

        int n_read = read(fd,readBuf,size);


        char *p = strstr(readBuf,"SCORE = ");
        if(p == NULL){
                printf("not found!\n");
                exit(-1);
        }

         p = p +strlen("SCORE = ");
        *p = '1';


        int n_write = write(fd,readBuf,strlen(readBuf));

        close(fd);


        return 0;
}



~                                                                                                                                                                                                                                  
~                                                                                                                                                                                                                                  
~                                                                                                                                                                                                                                  
~     

以上代码我们得到的结果没办法将原来的代码覆盖而是另起一行,如何解决?

提供两种方案:方案一:把光标移到头部再写

                            方案二:用TRUNC;将原来的内容删除

现在用方案一解决

 

代码优化:

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

int main(int argc , char *argv[])
{
        int fd;

        char *readBuf = NULL;

        if(argc != 2){
                printf("param error!\n");
                exit(-1);

        }

        fd = open(argv[1],O_RDWR);

        int size = lseek(fd,0,SEEK_END);
        lseek(fd,0,SEEK_SET);

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

        int n_read = read(fd,readBuf,size);


        char *p = strstr(readBuf,"SCORE = ");
        if(p == NULL){
                printf("not found!\n");
                exit(-1);
        }

         p = p +strlen("SCORE = ");
        *p = '1';


        lseek(fd,0,SEEK_SET);

        int n_write = write(fd,readBuf,strlen(readBuf));

        close(fd);


        return 0;
}



    注意事项:执行文件 ./a.out后面记得加文件名例如文件名为TEXT.config   运行用./a.out TEXT.config

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值