C++文件读写对象
2017-12-17 17:10
337 查看
将对象数组对象写入文件,并读取出来,并重新赋值。
#define _CRT_SECURE_NO_WARNINGS #include<iostream> #include <stdio.h> #include<cmath> using namespace std; struct STU { int sn; char name[20]; int score; } students; void main() { STU students[6]/* = { 1,"lihua", 100, 2, "wufang", 99, 3, "liu", 98 }*/; students[0].sn = 1; strcpy(students[0].name, "lihua"); students[0].score = 100; students[1].sn = 2; strcpy(students[1].name, "wufang"); students[1].score = 99; students[2].sn = 3; strcpy(students[2].name, "liu"); students[2].score = 98; FILE *fp,*fp1; fp = fopen("C:\\Users\\yihong\\Desktop\\a.txt","wb"); for (int i = 0; i < 3; i++) { fwrite(&students[i], sizeof(students[i]), 1, fp); } fclose(fp); fp1 = fopen("C:\\Users\\yihong\\Desktop\\a.txt", "rb"); STU studentsread; for (int i = 0; i < 3; i++) { fread(&students[i+3], sizeof(students[i]), 1, fp1); } fclose(fp); for (int i = 3; i < 6; i++) { cout << students[i].sn << " " << students[i].name << " " << students[i].score << endl; } }
相关文章推荐
- C++文件读写之对象的读写
- 基于链表的学生成绩管理系统——C++二进制文件读写string对象时出现的错误
- C++读写文件并排序
- C++文件读写
- C++读写文件操作
- 【转载】C++ 简单读写文本文件、统计文件的行数、读取文件数据到数组
- C++ 将对象写入文件 并读取
- 对C++文件组织及全局对象的理解
- 标准c++ 读写unicode文件
- C++读写文件
- C++文件读写详解(ofstream,ifstream,fstream)
- 关于C++文件读写的一些东西
- C++ 输入输出流 文本文件 二进制文件读写
- C++读写配置文件"*.ini
- C++读写配置文件 config类
- [C/C++标准库]_[初级]_[读写中文路径的文件--写入unicode字符串]
- c++读写文件和测试程序运行时间的例子
- C++读写本地文件(文件IO流)
- C++文件读写详解(ofstream,ifstream,fstream)
- C++读写文件