C语言读写文件

来源:互联网 发布:淘宝怎么购买二手店铺 编辑:程序博客网 时间:2024/05/17 08:38
//读文件int rFile(){        FILE *fp;    int flen;    char *p;    // 以只读方式打开文件    if((fp = fopen ("e:\\Record.db","r"))==NULL)    {        printf("\nfile open error\n");        exit(0);    }    fseek(fp,0L,SEEK_END);    // 定位到文件末尾    flen=ftell(fp);           // 得到文件大小    p=(char *)malloc(flen+1); // 根据文件大小动态分配内存空间    if(p==NULL){        fclose(fp);        return 0;    }    fseek(fp,0L,SEEK_SET); // 定位到文件开头    fread(p,flen,1,fp);    // 一次性读取全部文件内容    p[flen]=0;             // 字符串结束标志    printf("%s",p);    fclose(fp);    free(p);    return 0;}


 

//写文件 int wFile(){    FILE *fp;    fp = fopen( "e:\\1.txt", "w+" );    fprintf( fp, "hello world!" );    fclose( fp );    return 0;}



 

来源于:http://ileson.iteye.com/blog/1066741

原创粉丝点击