zoukankan      html  css  js  c++  java
  • Linux使用标准IO的调用函数,分3种形式实现

    /*
    根据cp命令的格式要求,设计一个类cp的功能程序,要求使用标准IO的调用函数,分3种形式实现,字符,行,块。并注意函数功能的分层
    */

    #include<stdio.h>
    #include <unistd.h>
    /*fgetc fputc 一个字符*/
    int cpchar(char *src, char *des)
    {
    FILE *fpsrc,*fpdes;
    char ch;

    fpsrc = fopen(src, "r");
    if(fpsrc==NULL)
    {
    printf("open src fail ");
    return -1;
    }

    fpdes = fopen(des, "w");
    if(fpdes==NULL)
    {
    printf("open des fail ");
    return -1;
    }

    while(1)
    {
    if((ch=fgetc(fpsrc))!=EOF)
    {
    fputc(ch,fpdes);
    printf("%c",ch);
    }
    else
    {
    break;
    }
    }

    fclose(fpsrc);
    fclose(fpdes);

    return 0;
    }

    /*fgets fputs 一行*/
    int cpline(char *src, char *des)
    {
    FILE *fpsrc,*fpdes;
    char buf[1024];

    fpsrc = fopen(src, "r");
    if(fpsrc==NULL)
    {
    printf("open src fail ");
    return -1;
    }

    fpdes = fopen(des, "w");
    if(fpdes==NULL)
    {
    printf("open des fail ");
    return -1;
    }

    while(1)
    {
    if(fgets(buf,1024,fpsrc)!=NULL)
    {
    fputs(buf,fpdes);
    printf("%s",buf);
    }
    else
    {
    break;
    }
    }

    fclose(fpsrc);
    fclose(fpdes);

    return 0;
    }

    /*fread fwrite 一块*/
    int cpblk(char *src, char *des)
    {
    FILE *fpsrc,*fpdes;
    char buf[1024];
    int num;

    fpsrc = fopen(src, "r");
    if(fpsrc==NULL)
    {
    printf("open src fail ");
    return -1;
    }

    fpdes = fopen(des, "w");
    if(fpdes==NULL)
    {
    printf("open des fail ");
    return -1;
    }

    while(1)
    {
    if((num=fread(buf,sizeof(char),1024,fpsrc))>0)
    {
    fwrite(buf,sizeof(char),num,fpdes);
    printf("%d ",num);
    }
    else
    {
    break;
    }
    }

    fclose(fpsrc);
    fclose(fpdes);

    return 0;
    }

  • 相关阅读:
    MySQL数据库优化的八种方式(经典必看)
    HTTP状态码详解
    一周学会HTML----Day03常用标签(下)
    一周学会HTML----Day02常用标签(上)
    SEO优化---10分钟学会建立高转化率的网站关键词库
    C# 命名的基本约定【转】
    arraylist是接口list的实现类
    API
    new与malloc区别(转)
    获取系统时间
  • 原文地址:https://www.cnblogs.com/chenglongxu/p/5284266.html
Copyright © 2011-2022 走看看