Chinaunix首页 | 论坛 | 博客
  • 博客访问: 33272
  • 博文数量: 9
  • 博客积分: 172
  • 博客等级: 入伍新兵
  • 技术积分: 105
  • 用 户 组: 普通用户
  • 注册时间: 2012-09-08 21:15
文章分类

全部博文(9)

文章存档

2012年(9)

最近访客

分类: 系统运维

2012-09-18 22:23:17

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;
}
阅读(1957) | 评论(1) | 转发(2) |
给主人留下些什么吧!~~

syw小武2012-09-25 21:00:11