• C language——to Replace a specified Line in a Text File


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

    点击(此处)折叠或打开

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

    2. #include <stdio.h>

    3. int main(void)
    4. {
    5.     FILE *fileptr1, *fileptr2;
    6.     char filechar[40];
    7.     char c;
    8.     int delete_line, temp = 1;

    9.     printf("Enter file name: ");
    10.     scanf("%s", filechar);

    11.     fileptr1 = fopen(filechar, "r");
    12.     //print the contents of file .
    13.     while ((c = getc(fileptr1)) != EOF)
    14.     {
    15.         printf("%c", c);
    16.     }

    17.     printf(" Enter line number to be deleted and replaced: ");
    18.     scanf("%d", &delete_line);

    19.     //take fileptr1 to start point.
    20.     rewind(fileptr1);
    21.     //open tempinterm.txt in write mode
    22.     fileptr2 = fopen("tempinterm.txt", "w");

    23.     while ((c = getc(fileptr1)) != EOF)
    24.     {
    25.         //till the line to be deleted comes,copy the content to other
    26.         if (temp != delete_line) {
    27.             putc(c, fileptr2);
    28.             while ((c = getc(fileptr1)) != 'n') putc(c, fileptr2);
    29.             putc('n', fileptr2);
    30.             temp++;
    31.         } else {
    32.             while ((c = getc(fileptr1)) != 'n');
    33.             //read and skip the line ask for new text
    34.             printf("Enter new text: ");

    35.             //flush the input stream
    36.             fflush(stdin);
    37.             getchar();    //delete blank line

    38.             while ((c = getchar()) != 'n') putc(c, fileptr2);
    39.             //take the data from user and place it in new file
    40.             putc('n', fileptr2);
    41.             temp++;
    42.         }
    43.     }

    44.     fclose(fileptr1);
    45.     fclose(fileptr2);
    46.     remove(filechar);
    47.     rename("tempinterm.txt", filechar);
    48.     fileptr1 = fopen(filechar, "r");
    49.     //reads the character from file
    50.     //until last character of file is encountered
    51.     while ((c = getc(fileptr1)) != EOF)
    52.     {
    53.         printf("%c", c);
    54.     }
    55.     fclose(fileptr1);
    56.     return 0;
    57. }
    [root@localhost 桌面]# gcc tmp.c -o tmp

    [root@localhost 桌面]# ./tmp
    Enter file name: hosts
    127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
    ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

    Enter line number to be deleted and replaced: 1
    Enter new text: asdf
    asdf
    ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
    [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>
    阅读(101) | 评论(0) | 转发(0) |
    给主人留下些什么吧!~~
    评论热议
  • 相关阅读:
    OpenMP
    linux下编写C++程序播放音频
    Canny Edge Detector
    部署服务器
    第五周--论文泛读
    AI研习社“看图猜作者”优秀代码技术总结
    Neural Multimodal Cooperative Learning Toward Micro-Video Understanding学习笔记
    第二次作业:卷积神经网络 part 2
    循环神经网络
    ORB-SLAM学习笔记
  • 原文地址:https://www.cnblogs.com/ztguang/p/12649499.html
Copyright © 2020-2023  润新知