奋斗中的cc 2018-10-28 04:51 采纳率: 0%
浏览 998
已采纳

VS2017对二进制文件写入和读出是乱麻麻

代码:#include
#include
using namespace std;
struct student
{
int num;
char name[20];
float score;
};
int main()
{
student stud[5] = { 1001,"Li",85,1002,"Fan",97.5,1004,"Wang",54,1006,"Tan",76.5,1010,"Ling",96 };
fstream iofile("stud.dat", ios::in | ios::out | ios::binary);
if (!iofile)
{
cerr << "open error!" << endl;
abort();
}
for (int i = 0; i < 5; i++)
iofile.write((char*)&stud[i], sizeof(stud[i]));
student stud1[5];
for (int i = 0; i < 5; i = i + 2)
{
iofile.seekg(i * sizeof(stud[i]), ios::beg);
iofile.read((char*)&stud[i / 2], sizeof(stud1[0]));
cout << stud1[i / 2].num << " " << stud1[i / 2].name << " " << stud1[i / 2].score << endl;
}
cout << endl;
stud[2].num = 1012;
strcpy_s(stud[2].name, "Wu");
stud[2].score = 60;
iofile.seekg(2 * sizeof(stud[0]), ios::beg);
iofile.write((char*)&stud[2], sizeof(stud[2]));
iofile.seekg(0, ios::beg);
for (int i = 0; i < 5; i++)
{
iofile.read((char*)&stud[i], sizeof(stud1[i]));
cout << stud1[i].num << " " << stud1[i].name << " " << stud1[i].score << endl;
}
iofile.close();
return 0;
}

运行结果:图片说明

  • 写回答

1条回答 默认 最新

  • threenewbee 2018-10-28 05:22
    关注
     // Q704572.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    
    #include<fstream> 
    #include<iostream>
    using namespace std;
    struct student
    {
    int num;
    char name[20];
    float score;
    };
    int main()
    {
    student stud[5] = { 1001,"Li",85,1002,"Fan",97.5,1004,"Wang",54,1006,"Tan",76.5,1010,"Ling",96 };
    fstream iofile("c:\\stud.dat", ios::in | ios::out | ios::binary);
    if (!iofile)
    {
    cerr << "open error!" << endl;
    abort();
    }
    for (int i = 0; i < 5; i++)
    iofile.write((char*)&stud[i], sizeof(student));
    student stud1[5];
    for (int i = 0; i < 5; i++)
    {
    iofile.seekg(i * sizeof(student), ios::beg);
    iofile.read((char*)&stud1[i], sizeof(student));
    cout << stud1[i].num << " " << stud1[i].name << " " << stud1[i].score << endl;
    }
    cout << endl;
    stud[2].num = 1012;
    strcpy_s(stud[2].name, "Wu");
    stud[2].score = 60;
    iofile.seekg(2 * sizeof(stud[0]), ios::beg);
    iofile.write((char*)&stud[2], sizeof(stud[2]));
    iofile.seekg(0, ios::beg);
    for (int i = 0; i < 5; i++)
    {
    iofile.read((char*)&stud[i], sizeof(stud1[i]));
    cout << stud1[i].num << " " << stud1[i].name << " " << stud1[i].score << endl;
    }
    iofile.close();
    return 0;
    }
    

    图片说明

    如果问题得到解决,请点我回答右边的采纳,谢谢


    补充下,后面的也不对,你
    stud[2].num = 1012;
    strcpy_s(stud[2].name, "Wu");
    stud[2].score = 60;
    iofile.seekg(2 * sizeof(stud[0]), ios::beg);
    iofile.write((char*)&stud[2], sizeof(stud[2]));
    更新的是stud
    但是最后循环输出的是stud1,所以更新了看不到变化。
    这个你自己修改下吧。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 已采纳回答 4月3日

悬赏问题

  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密
  • ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴
  • ¥15 下列c语言代码为何输出了多余的空格