• 【C/C++】标准IO操作


    fwrite实现文件复制

     1 //@ author 成鹏致远
     2 //@ net http://infodown.tap.cn
     3 //@ qq 552158509
     4 //@ blog lcw.cnblogs.com
     5 
     6 #include <stdio.h>
     7 #include <stdlib.h>
     8 #include <string.h>
     9 
    10 #define BUFSIZE 100
    11 
    12 int main(int argc, char * argv[])
    13 {
    14     FILE *read_fp,*write_fp;
    15     char buf[BUFSIZE];
    16 
    17     if(3 != argc)
    18     {
    19         printf("Usage:%s <origin_filename> <target_filename> 
    ",argv[0]);
    20         exit(1);
    21     }
    22     if(NULL == (read_fp = fopen(argv[1],"r")))
    23     {
    24         perror("malloc");
    25         exit(1);
    26     }
    27     if(NULL == (write_fp = fopen(argv[2],"w")))
    28     {
    29         perror("malloc");
    30         exit(1);
    31     }
    32 
    33     while(!feof(read_fp) && !ferror(read_fp))
    34     {
    35         bzero(buf,BUFSIZE);//在使用前一定要先清缓冲
    36         fread(buf,BUFSIZE,1,read_fp);//读取文件
    37         fwrite(buf,strlen(buf),1,write_fp);//写文件,注意要用strlen,因为最后一次可以装不满缓冲区
    38     }
    39 
    40     fclose(read_fp);
    41     fclose(write_fp);
    42 
    43 
    44     return 0;
    45 }
    View Code

    fgets实现文件复制

     1 //@ author 成鹏致远
     2 //@ net http://infodown.tap.cn
     3 //@ qq 552158509
     4 //@ blog lcw.cnblogs.com
     5 
     6 //copy file by line
     7 
     8 #include <stdio.h>
     9 #include <stdlib.h>
    10 
    11 #define MAXSIZE 1024
    12 
    13 int main(int argc, char * argv[])
    14 {
    15     FILE *read_fp,*write_fp;
    16     char buf[MAXSIZE];
    17 
    18     if(3 != argc)
    19     {
    20         printf("Usage:%s <origin_filename> <target_filename> 
    ",argv[0]);
    21         exit(1);
    22     }
    23     if(NULL == (read_fp = fopen(argv[1],"r")))
    24     {
    25         perror("malloc");
    26         exit(1);
    27     }
    28     if(NULL == (write_fp = fopen(argv[2],"w")))
    29     {
    30         perror("malloc");
    31         exit(1);
    32     }
    33 
    34     while(NULL != fgets(buf,MAXSIZE,read_fp))//fgets()浼氭妸'
    '绠楀湪缂撳啿鍖轰腑
    35     {
    36         fputs(buf,write_fp);//paste file
    37     }
    38 
    39     fclose(read_fp);
    40     fclose(write_fp);
    41 
    42     return 0;
    43 }
    View Code

    fgetc实现文件

     1 //@ author 成鹏致远
     2 //@ net http://infodown.tap.cn
     3 //@ qq 552158509
     4 //@ blog lcw.cnblogs.com
     5 
     6 //copy file by char
     7 
     8 #include <stdio.h>
     9 #include <stdlib.h>
    10 
    11 
    12 int main(int argc, char * argv[])
    13 {
    14     FILE *read_fp,*write_fp;
    15     char tmp;
    16 
    17     if(3 != argc)
    18     {
    19         printf("Usage:%s <origin_filename> <target_filename> 
    ",argv[0]);
    20         exit(1);
    21     }
    22     if(NULL == (read_fp = fopen(argv[1],"r")))
    23     {
    24         perror("malloc");
    25         exit(1);
    26     }
    27     if(NULL == (write_fp = fopen(argv[2],"w")))
    28     {
    29         perror("malloc");
    30         exit(1);
    31     }
    32 
    33     while(EOF !=(tmp= fgetc(read_fp)))//copy file
    34     {
    35         fputc(tmp,write_fp);//paste file
    36     }
    37 
    38     fclose(read_fp);
    39     fclose(write_fp);
    40 
    41 
    42     return 0;
    43 }
    View Code
  • 相关阅读:
    C++结构体内重载、this指针和友元函数(初步了解)
    数据结构—造树计划—二叉搜索树
    PTA顺序的分数
    PTA兼容任务
    PTA航船
    UML-基于GRASP对象设计步骤
    UML-设计对象时涉及的制品有哪些?
    UML-什么是用例实现(场景实现)?
    UML-如何使用GRASP进行对象设计?
    日志总结
  • 原文地址:https://www.cnblogs.com/lcw/p/3235099.html
Copyright © 2020-2023  润新知