构建一个结构体struct student,包括两个成员:id和avescore。
构建一个struct student类型的动态数组(new 操作),元素个数由用户输入(要求3个)。
输入数据存入数组并显示出来({1,5;2,8;3,10})
使用文本模式通过I/O流把数组内容存入磁盘文件:student.txt
从student.txt中读出数据,采用二进制模式存入文件student.bin
使用VS打开两个文件,比较两个文件的区别
c++ io流输入输出问题求解
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
2条回答 默认 最新
- nokianasty 2018-05-02 09:17关注
C++ 代码已经实现,代码调试通过。请速度采用,急用C币下载!!
#include<iostream> #include<fstream> using namespace std; struct student { int id; int avescore; }; int main() { int numbers = 0; cout << "请输入元素个数:"; cin >> numbers; student* pArrStudent = new student[numbers]; for (int i = 0; i < numbers; i++) { cout << "请输入第" << i + 1 << "个元素id和avescore,空格分隔:"; int id = 0; int avescore = 0; cin >> id >> avescore; pArrStudent[i].id = id; pArrStudent[i].avescore = avescore; } //显示输入的数据 cout << "({"; for (int i = 0; i < numbers; i++) { if (i < numbers - 1) { cout << pArrStudent[i].id << "," << pArrStudent[i].avescore << ";"; } else { cout << pArrStudent[i].id << "," << pArrStudent[i].avescore; } }; cout << "})"; //存入磁盘文件:student.txt ofstream oFile(L"student.txt"); oFile.write((char*)pArrStudent, sizeof(student)*numbers); oFile.close(); //从student.txt中读出数据 student* pArrStudentTemp = new student[numbers]; ifstream iFileTemp(L"student.txt"); iFileTemp.read((char*)pArrStudentTemp, sizeof(student)*numbers); iFileTemp.close(); //采用二进制模式存入文件student.bin ofstream oFileTemp(L"student.bin", ios::binary); oFileTemp.write((char*)pArrStudent, sizeof(student)*numbers); oFileTemp.close(); delete []pArrStudent; delete[]pArrStudentTemp; return 0; };
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 宇视监控服务器无法登录
- ¥15 PADS Logic 原理图
- ¥15 PADS Logic 图标
- ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
- ¥15 DruidDataSource一直closing
- ¥20 气象站点数据求取中~
- ¥15 如何获取APP内弹出的网址链接
- ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
- ¥50 STM32单片机传感器读取错误
- ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据