Configuration file of Linux file I/O modifier (summary of file programming III)

Keywords: Programming Linux

Document programming summary III

Project Name: configuration file of Linux file I/O modifier

Objective: to further exercise the use of linux file I/O programming basic functions: open, read, write, close, lseek;

Before the project starts, we will introduce the string STR function under Linux C

Function function: find the first occurrence of needle from the string haystack, but the function does not compare the NULL terminator.
  
Return Description: returns a pointer to the first needle position, or NULL if it is not found.

SYNOPSIS
       #include <string.h>

       char *strstr(const char *haystack, const char *needle);//Parameter Description: haystack is a pointer to a source string and needle is a pointer to a destination string.
        

       #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.
*/

Programming diagram of this function:


       

Preliminary implementation of the code:

#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;
}



~                                                                                                                                                                                                                                  
~                                                                                                                                                                                                                                  
~                                                                                                                                                                                                                                  
~     

The result of the above code can't cover the original code but start another line. How to solve it?

There are two solutions: one is to move the cursor to the head and write again

Scheme 2: use TRUNC to delete the original content

Now use solution one

 

Code optimization:

#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;
}



Note: Please add the file name after the execution file. / a.out. For example, the file name is TEXT.config for running. / a.out TEXT.config

 

 

Posted by kentish on Wed, 04 Dec 2019 04:56:24 -0800