C language implementation code for modifying specific lines in a text file

Source: Internet
Author: User

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.

Related Article

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.