A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© Brisingr 中级黑马   /  2015-10-12 23:00  /  904 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

为什么结构体数组写入文件,是写入的二进制文件?我没有用wb啊

代码:int main(int argc, const char * argv[]) {
    struct student std[3] = {{"张三丰",28,60},{"胡斐",25,70.5},{"诸葛亮",45,100}};
    FILE *fp = fopen("a.txt", "w+");
    if (fp!=NULL) {
        //fseek(fp, sizeof(struct student),SEEK_SET);
        fwrite(std, sizeof(struct student), 3, fp);
        printf("写入成功!\n");
        struct student std1[3];
        fseek(fp, -sizeof(struct student), SEEK_END);
        fread(std1, sizeof(struct student), 1, fp);
        for (int i=0; i<1; i++) {
            printf("%s,%d,%.2f\n",std1[i].name,std[i].age,std1[i].score);
        }
    }
    fclose(fp);
    return 0;
}

3 个回复

倒序浏览
自己先顶
回复 使用道具 举报
没有人嘛嘛大神快来啊
回复 使用道具 举报
人啊人啊快来人啊
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马