Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6549811
  • 博文数量: 1159
  • 博客积分: 12444
  • 博客等级: 上将
  • 技术积分: 12570
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-13 21:34
文章分类

全部博文(1159)

文章存档

2016年(126)

2015年(350)

2014年(56)

2013年(91)

2012年(182)

2011年(193)

2010年(138)

2009年(23)

分类: C/C++

2016-01-16 20:50:37

[root@localhost 桌面]# gedit tmp.c

点击(此处)折叠或打开

  1. // C Language to Replace a specified Line in a Text File

  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>

  5. //filename: "/etc/hosts"
  6. //str: "mpe.localhost"
  7. //return the matched line number
  8. int findline(char *filename, char *str)
  9. {
  10.     // open the file for reading
  11.     FILE *file = fopen(filename, "r");

  12.     // make sure the file opened properly
  13.     if(NULL == file)
  14.     {
  15.         fprintf(stderr, "Cannot open file: %s\n", filename);
  16.         return 1;
  17.     }

  18.     // set up the buffer to read the line into. Don't worry too much
  19.     // if some of the lines are longer than 80 characters - the buffer
  20.     // will be made bigger if need be.
  21.     size_t buffer_size = 80;
  22.     char *buffer = malloc(buffer_size * sizeof(char));

  23.     // read each line and print it to the screen
  24.     int line_number = 0, find = 0;
  25.     while(-1 != getline(&buffer, &buffer_size, file))
  26.     {
  27.         line_number++;
  28.         if(NULL != strstr(buffer, str))
  29.         {
  30.             find = 1;
  31.             break;
  32.         }
  33.         //printf("%d: %s", line_number, buffer);
  34.     }

  35.     if(find == 0) line_number = 0;

  36.     fclose(file);
  37.     free(buffer);

  38.     return line_number;
  39. }

  40. void replaceline(char *filename, int delete_line, char *newcontent)
  41. {
  42.     FILE *fileptr1, *fileptr2;
  43.     //char filename[40];
  44.     char c;
  45.     int temp = 1;

  46.     fileptr1 = fopen(filename, "r");
  47.     //print the contents of file .
  48.     while ((c = getc(fileptr1)) != EOF)
  49.     {
  50.         printf("%c", c);
  51.     }

  52.     //take fileptr1 to start point.
  53.     rewind(fileptr1);
  54.     //open tempinterm.txt in write mode
  55.     fileptr2 = fopen("tempinterm.txt", "w");

  56.     while ((c = getc(fileptr1)) != EOF)
  57.     {
  58.         //till the line to be deleted comes,copy the content to other
  59.         if (temp != delete_line) {
  60.             putc(c, fileptr2);
  61.             while ((c = getc(fileptr1)) != '\n') putc(c, fileptr2);
  62.             putc('\n', fileptr2);
  63.             temp++;
  64.         } else {
  65.             while ((c = getc(fileptr1)) != '\n');    //read and skip the line
  66.             //while ((c = getchar()) != '\n') putc(c, fileptr2);
  67.             while(*newcontent != '\0') putc(*newcontent++, fileptr2);
  68.             putc('\n', fileptr2);
  69.             temp++;
  70.         }
  71.     }

  72.     //append a new line at the end of file
  73.     if(delete_line == 0)
  74.     {
  75.         while(*newcontent != '\0') putc(*newcontent++, fileptr2);
  76.         putc('\n', fileptr2);
  77.     }

  78.     fclose(fileptr1);
  79.     fclose(fileptr2);
  80.     remove(filename);
  81.     rename("tempinterm.txt", filename);
  82. }

  83. int main(void)
  84. {
  85.     FILE *fileptr1;
  86.     char c;

  87.     char filename[6] = "hosts";

  88.     int line = findline(filename, "mpe.localhost");
  89.     replaceline(filename, line, "10.108.162.227 mpe.localhost");

  90.     fileptr1 = fopen(filename, "r");
  91.     //reads the character from file
  92.     //until last character of file is encountered
  93.     while ((c = getc(fileptr1)) != EOF) printf("%c", c);
  94.     fclose(fileptr1);
  95.     return 0;
  96. }
[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 桌面]#





阅读(1162) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~