The project needs to be implemented to modify the contents of the file, so there are the following test procedures, which encountered a lot of problems, recorded in this one.
In fact, the implementation principle is very simple, first of all, to achieve the desired field to navigate to the line, and then modify the specified field. After I got in touch with shell programming, I felt that shell programming would be easy to implement (you can use the SED command on the internet, haven't contacted, have time to get it). But because C language is a process-oriented language, it needs the characteristics of sequential execution, so it encounters a lot of trouble in implementation.
First, the contents of the file-data file that need to be modified are as follows:
00:00:00:00:00:00-192.168.1.1
00:00:00:00:00:01-192.168.1.2
00:00:00:00:00:02-192.168.1.3
00:00:00:00:00:03-192.168.1.4
Ps:1. The content of each row is a set of data,-the previous content is the MAC address,-The following content is the corresponding IP address.
2. Do not leave extra space at the end of each line (as for why this is done, let me explain below)
The function I want to implement is to modify the IP address of the MAC address I specified.
For example, I want to modify the IP address of 00:00:00:00:00:01 to 192.168.1.5
The implementation code is as follows:
#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct LOCALADDR {char mac[18];
Char ip[16];
}LOCALADDR;
int main () {FILE *fp;
Char line_buf[512], buf1[512], buf2[512];
int Line_len, Len;
Localaddr localaddr;
strcpy (Localaddr.mac, "00:00:00:00:00:01");
strcpy (Localaddr.ip, "192.168.1.5");
if (fp = fopen ("Data", "r+")) = = NULL) {perror ("fopen");
Exit (1);
while (Fgets (Line_buf, the FP)) {Line_len = strlen (LINE_BUF);
Len + + Line_len; SSCANF (Line_buf, "%[^-]-%[0-9,.]", BUF1, BUF2); Assign the previous content (MAC address) to BUF1, after which the content (IP address) is given to Buf2 if (!strcmp (Localaddr.mac, buf1)) {len-= (strlen (line_buf) + 1);
Calculates the line's beginning position printf ("Len =%d\n", Len);
if (fseek (FP, Len, Seek_set) < 0)//positioning {perror ("fseek");
Exit (1);
} strcpy (Buf2, LOCALADDR.IP);
strcat (BUF1, "-");
strcat (BUF1, BUF2);
fprintf (FP, "%s", BUF1);
Fclose (FP);
return;
} exit (0); }
After compiling, the contents of the data file become:
00:00:00:00:00:00-192.168.1.1
00:00:00:00:00:01-192.168.1.5
00:00:00:00:00:02-192.168.1.3
00:00:00:00:00:03-192.168.1.4
The test was successful.
Now,come the question:
If the IP address requirements are modified to 192.168.1.123, after the program is compiled, the contents of the data file become:
00:00:00:00:00:00-192.168.1.1
00:00:00:00:00:01-192.168.1.1230:00:00:00:00:02-192.168.1.3
00:00:00:00:00:03-192.168.1.4
What's going on.
Analysis:
IP host Address field from the original "2" into "5" when the length has not changed, so directly covered the original "2", the other unchanged.
However, when "2" becomes "123", the length is inconsistent and needs to occupy 2 more bytes. So, in addition to the "1" in "123", which covers the original "2", "2" in "123" occupies the space of the line break, causing the contents of the third row to run on the second line, and "123" in "3" The original third line of the first "0" space occupied, so there have been above such accidents.
Workaround:
My solution is also simple to ensure that the length of each line in the data file is >=34: (MAC address length) +15 (maximum IP address length) +1 (one "-") +1 (line break)
It's not a matter of fixing the code, but it's a solution, isn't it.