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

扫描二维码关注公众号,回复: 5672258 查看本文章

猜你喜欢

转载自blog.csdn.net/qq_41899773/article/details/88824897