[root@localhost 桌面]# gedit tmp.c
-
// C Language to Replace a specified Line in a Text File
-
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <string.h>
-
-
//filename: "/etc/hosts"
-
//str: "mpe.localhost"
-
//return the matched line number
-
int findline(char *filename, char *str)
-
{
-
// open the file for reading
-
FILE *file = fopen(filename, "r");
-
-
// make sure the file opened properly
-
if(NULL == file)
-
{
-
fprintf(stderr, "Cannot open file: %s\n", filename);
-
return 1;
-
}
-
-
// set up the buffer to read the line into. Don't worry too much
-
// if some of the lines are longer than 80 characters - the buffer
-
// will be made bigger if need be.
-
size_t buffer_size = 80;
-
char *buffer = malloc(buffer_size * sizeof(char));
-
-
// read each line and print it to the screen
-
int line_number = 0, find = 0;
-
while(-1 != getline(&buffer, &buffer_size, file))
-
{
-
line_number++;
-
if(NULL != strstr(buffer, str))
-
{
-
find = 1;
-
break;
-
}
-
//printf("%d: %s", line_number, buffer);
-
}
-
-
if(find == 0) line_number = 0;
-
-
fclose(file);
-
free(buffer);
-
-
return line_number;
-
}
-
-
void replaceline(char *filename, int delete_line, char *newcontent)
-
{
-
FILE *fileptr1, *fileptr2;
-
//char filename[40];
-
char c;
-
int temp = 1;
-
-
fileptr1 = fopen(filename, "r");
-
//print the contents of file .
-
while ((c = getc(fileptr1)) != EOF)
-
{
-
printf("%c", c);
-
}
-
-
//take fileptr1 to start point.
-
rewind(fileptr1);
-
//open tempinterm.txt in write mode
-
fileptr2 = fopen("tempinterm.txt", "w");
-
-
while ((c = getc(fileptr1)) != EOF)
-
{
-
//till the line to be deleted comes,copy the content to other
-
if (temp != delete_line) {
-
putc(c, fileptr2);
-
while ((c = getc(fileptr1)) != '\n') putc(c, fileptr2);
-
putc('\n', fileptr2);
-
temp++;
-
} else {
-
while ((c = getc(fileptr1)) != '\n'); //read and skip the line
-
//while ((c = getchar()) != '\n') putc(c, fileptr2);
-
while(*newcontent != '\0') putc(*newcontent++, fileptr2);
-
putc('\n', fileptr2);
-
temp++;
-
}
-
}
-
-
//append a new line at the end of file
-
if(delete_line == 0)
-
{
-
while(*newcontent != '\0') putc(*newcontent++, fileptr2);
-
putc('\n', fileptr2);
-
}
-
-
fclose(fileptr1);
-
fclose(fileptr2);
-
remove(filename);
-
rename("tempinterm.txt", filename);
-
}
-
-
int main(void)
-
{
-
FILE *fileptr1;
-
char c;
-
-
char filename[6] = "hosts";
-
-
int line = findline(filename, "mpe.localhost");
-
replaceline(filename, line, "10.108.162.227 mpe.localhost");
-
-
fileptr1 = fopen(filename, "r");
-
//reads the character from file
-
//until last character of file is encountered
-
while ((c = getc(fileptr1)) != EOF) printf("%c", c);
-
fclose(fileptr1);
-
return 0;
-
}
[root@localhost 桌面]# gcc tmp.c -o tmp
[root@localhost 桌面]# ./tmp
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
10.108.162.227 mpe.localhost
[root@localhost 桌面]#
阅读(1222) | 评论(0) | 转发(0) |