Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1682670
  • 博文数量: 1493
  • 博客积分: 38
  • 博客等级: 民兵
  • 技术积分: 5834
  • 用 户 组: 普通用户
  • 注册时间: 2009-08-19 17:28
文章分类

全部博文(1493)

文章存档

2016年(11)

2015年(38)

2014年(137)

2013年(253)

2012年(1054)

2011年(1)

分类:

2012-09-19 08:56:45

原文地址:i/o 的简单复制 作者:syw小武

1从文件中读一行
#include
int main(int argc, char *argv[])
{
    FILE *oldfile, *newfile;
    //oldfile="a.txt";
    //newfile="b.txt";
    oldfile=fopen("a.txt","r");
    if(oldfile == NULL){
        printf("the fild is open error");
        return -1;
    }
    else{
//        printf("the a.txt is " oldfile);
        printf("aa");
    }
    newfile=fopen("b.txt", "w ");
    if(newfile == NULL){
        printf("the b.txt is error");
    }
    
    char p[1024];
    while (fgets(p,sizeof(p),oldfile)!= NULL){
        fputs(p , newfile);
}

    fclose(oldfile);
    fclose(newfile);
     return 0;
}

2  读一个字符

#include

int main(int argc, char *argv[])
{
    FILE *fp1,*fp2;
    fp1=fopen("b.c", "w ");
    if(fp1 == NULL){
        perror("fopen");
    }
    else{
        printf("ok !!");
    }

    fputs("a", fp1);
    fputs("b", fp1);
    fputs("0xff", fp1);

        rewind(fp1);

//    fputs(EOF, fp1);
     fp2=fopen("b1.c", "w ");
    if(fp2 == NULL){
        perror("fopen");
    }
    else{
        printf("b1.c is ok \n");
    }
        int c;
        while((c=fgetc(fp1))!=EOF){
        fputc(c, fp2);
    }

    fclose(fp1);
    fclose(fp2);
    return 0;
}
阅读(127) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~