C language implementation modifies a specific line in a text file

Source: Internet
Author: User
Tags printf strlen

Recently because the project needs to implement the function of modifying the file, so the blogger carefully consulted some information, but, unfortunately, and not too much harvest.

OK, first of all, let me describe the following functional requirements:

In fact, it is very simple, is the shell in the SED command of the C language implementation, to navigate to the required field of the line, and then modify the required content. However, because C language is a process-oriented language, the need for sequential implementation of the characteristics, so, the implementation of a lot of trouble, where the blogger to achieve the process described below, so that you can refer to.

Problem Description:

Text content:

wireless.1.authmode=1  
wireless.1.compression=0  
wireless.1.current_ap=ssid12  
Wireless.1.current_ state=1  
wireless.1.devname=ath0  
wireless.1.enable_slave1_status=disabled  
wireless.1.enable_slave2_ status=disabled  
wireless.1.enable_slave3_status=disabled

All I need to do is modify the contents of line fourth to make it:

Wireless.1.current_state=0

The problem seems simple, the realization process is quite a lot of trouble ...

Here I give the implementation code, and the comment is added to the code:

* * * Author:dlutbrucezhang * date:2013.06.24/#include <stdio.h> #include <stdlib.h> #include <s tring.h> #include <unistd.h> int main () {/* *linebuffer: Reads a buffer *buffer1 in a file: The first field in a row is stored  
    into buffer *buffer2: buffer for second field in line/char linebuffer[512] = {0};  
    Char buffer1[512] = {0};  
          
    Char buffer2[512] = {0};  
    int line_len = 0;  
    int len = 0;  
          
    int res;  
    * * * CC.CFG is the filename, r+ representative can read and write files */file *FP = fopen ("Cc.cfg", "r+");  
        if (fp = NULL) {printf ("open error");  
    return-1;  
        while (Fgets (Linebuffer, the FP)) {Line_len = strlen (Linebuffer);  
        Len + + Line_len; * * * buffer1=wireless.1.current_state * buffer2=1 * * SSCANF (Linebuffer, "%[^=]=%[^=")  
        ", Buffer1,buffer2); 
      if (!strcmp ("Wireless.1.current_state", Buffer1)) {/*       * the "head"/len-= strlen (linebuffer) of the write position is required because the location of the required writing has been found;  
            * * To implement the offset of the file location, prepare for writing the file/Res = fseek (FP, Len, Seek_set);  
                if (Res < 0) {perror ("fseek");  
            return-1;  
            } strcpy (buffer2, "= 0");  
            /*strcat (buffer1, "="); * * STRCAT (Buffer1, buffer2);  
            printf ("%d", strlen (Buffer1));  
            * * Write the file, deposit the required content * * fprintf (FP, "%s", buffer1);  
            Fclose (FP);  
        Return  
} return 0; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.