zoukankan      html  css  js  c++  java
  • C language——to Replace a specified Line in a Text File—V2

    [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 ", 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 桌面]#





    <script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
    阅读(69) | 评论(0) | 转发(0) |
    给主人留下些什么吧!~~
    评论热议
  • 相关阅读:
    LeetCode Array Easy 414. Third Maximum Number
    LeetCode Linked List Medium 2. Add Two Numbers
    LeetCode Array Easy 283. Move Zeroes
    LeetCode Array Easy 268. Missing Number
    LeetCode Array Easy 219. Contains Duplicate II
    LeetCode Array Easy 217. Contains Duplicate
    LeetCode Array Easy 189. Rotate Array
    LeetCode Array Easy169. Majority Element
    LeetCode Array Medium 11. Container With Most Water
    LeetCode Array Easy 167. Two Sum II
  • 原文地址:https://www.cnblogs.com/ztguang/p/12649502.html
Copyright © 2011-2022 走看看