Chinaunix首页 | 论坛 | 博客
  • 博客访问: 505003
  • 博文数量: 257
  • 博客积分: 1666
  • 博客等级: 上尉
  • 技术积分: 1535
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-02 23:02
文章分类

全部博文(257)

文章存档

2013年(2)

2012年(255)

分类:

2012-09-20 12:34: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;
}
阅读(388) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~