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:
Copy Code code 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
All I need to do is modify the contents of line fourth to make it:
Copy Code code as follows:
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:
Copy Code code as follows:
/*
* Author:dlutbrucezhang
* date:2013.06.24
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main ()
{
/*
*linebuffer: Read a buffer stored in a file
*buffer1: Buffer for the first field in a row
*buffer2: Buffer for the second field in a row
*/
Char linebuffer[512] = {0};
Char buffer1[512] = {0};
Char buffer2[512] = {0};
int line_len = 0;
int len = 0;
int res;
/*
* Cc.cfg is a 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" of the write location is required because the location where you want to write is found
*/
Len-= strlen (Linebuffer);
/*
* Implement the offset of file location, prepare for writing files
*/
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 deposit the required content
*/
fprintf (FP, "%s", buffer1);
Fclose (FP);
Return
}
}
return 0;
}
Save file Name: MY_SED.C
The operation effect is as follows:
Let's see that the contents of the file have changed to:
Copy Code code 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 efficiency is higher here, because instead of loading the contents of the entire file into the buffer, reading the line, knowing the match, and then using the feature of the write file, you can directly overwrite the content, thus completing the required function.