Okay. First, let me describe the functional requirements:
In fact, it is very simple, that is, the C language implementation of the sed command in Shell, to locate the line of the required field, and then modify it to the required content. However, since the C language is a process-oriented language and requires sequential execution, there is a lot of trouble in implementation. Here, the blogger will describe the implementation process as follows for your reference.
Problem description:
Text Content:
Copy codeThe Code is as follows: 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
What I need to do is modify the content of the fourth line to make it:
Copy codeThe Code is as follows: wireless.1.current _ state = 0
The problem seems simple, and the implementation process is quite complicated...
The implementation code is provided here. The comments have been added to the Code:
Copy codeThe Code is as follows :/*
* Author: DLUTBruceZhang
* Date: 2013.06.24
*/
# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
# Include <unistd. h>
Int main ()
{
/*
* Linebuffer: Buffer used to read a row in a file.
* Buffer1: the buffer for storing the first field in a row
* Buffer2: the buffer for storing the Second Field in a row
*/
Char linebuffer [512] = {0 };
Char buffer1 [1, 512] = {0 };
Char buffer2 [512] = {0 };
Int line_len = 0;
Int len = 0;
Int res;
/*
* Cc. cfg is the file name. r + indicates that the file can be read and written.
*/
FILE * fp = fopen ("cc. cfg", "r + ");
If (fp = NULL)
{
Printf ("open error ");
Return-1;
}
While (fgets (linebuffer, 512, 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 ))
{
/*
* Because the location to be written has been found, you need to write the "Header" of the location"
*/
Len-= strlen (linebuffer );
/*
* Offset the file location to prepare for file writing.
*/
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 a file and store the required content
*/
Fprintf (fp, "% s", buffer1 );
Fclose (fp );
Return;
}
}
Return 0;
}
Save the file name as my_sed.c.
The running effect is as follows:
Let's see that the content in the file has changed:
Copy codeThe Code is as follows: wireless.1.authmode = 1
Wireless.1.compression = 0
Wireless.1.current _ ap = ssid12
Wireless.1.current _ state = 0
Wireless.1.enable _ slave1_status = disabled
Wireless.1.enable _ slave2_status = disabled
Wireless.1.enable _ slave3_status = disabled
Implementation principle:
The implementation efficiency here is relatively high, because it is not to load the content of the entire file into the buffer, but to read a row, know the matching, and then use the characteristics of writing the file, directly overwrite the written content to complete the required functions.