以一个学生信息的结构体数组为例。

#include<iostream>
#include<string>
#include<fstream>
using namespace std;
struct Student{
public:
int n;//学号
char name[20];//姓名
int age;//年龄
};
int main(){
const int N=3;
Student a[N];

//下面进行输入每个学生信息
for(int i=0;i<N;i++){
cin>>a[i].n>>a[i].name>>a[i].age;
cout<<a[i].n<<" "<<a[i].name<<" "<<a[i].age<<endl;
}

string filepath="a.txt";
//结构体对象数组二进制方式写入txt文档
ofstream fout;
fout.open(filepath,ios::binary);
if(!fout){
cout<<"打开文件失败!"<<endl;
system("pause");
return 0;
}
else{
for(int i=0;i<N;i++){
fout.write((char*)(&a[i]),sizeof(a[i]));
}
fout.close();
return 1;
}

//结构体对象数组二进制方式从txt文档读取出来
ifstream fread;
fread.open(filepath,ios::binary);
if(!fread){
cout<<"文件打开错误!"<<endl;
return 0;
}
else{
cout<<"文件打开成功!"<<endl;
for(int i=0;i<N;i++){
fread.read((char*)(&a[i]),sizeof(a[i]));
cout<<a[i].n<<" "<<a[i].name<<" "<<a[i].age<<endl;
}
system("pause");
return 1;
}

}

C++结构体对象数组的二进制方式读写的更多相关文章

  1. 结构体:探析C#文件方式读写结构体

    最近直在研究Net Micro Framework字体文件(tinyfnt)由于tinyfnt文件头部有段描述数据所以很想 定义个结构体像VC样直接从文件中读出来省得用流个个解析很是麻烦 没有想到在中 ...

  2. C# 篇基础知识2——运算符、类型转换、流程控制、枚举、结构体和数组、函数

    1.运算符.类型转换 计算某年y某月m某日d是周几的基姆拉尔森公式公式:int week = (d + 2*m + 3*(m + 1)/5 + y + y/4 - y/100 + y/400 + 1) ...

  3. matlab结构体、数组和单元数组类型的创建

    matlab结构体.数组和单元数组类型的创建 @ 目录 matlab结构体.数组和单元数组类型的创建 matlab结构体类型 数组类型 单元数组类型 matlab结构体类型 通过字段赋值创建结构体 创 ...

  4. C语言定义结构体指针数组并初始化;里面全是结构体的地址

    #include <stdio.h> #include <string.h> struct tells;//声明结构体 struct info { char *infos; } ...

  5. golang 修改数组中结构体对象的值的坑

    对对象数组逐个修改元素属性时候没有成功,代码如下: for _, configure := range configures { configure.Price = specPriceMap[conf ...

  6. C#学习笔记(七):结构体、数组、冒泡排序和调试

    结构体 结构体不能重写默认无参构造函数 一位数组 using System; using System.Collections.Generic; using System.Linq; using Sy ...

  7. C# Struct结构体里数组长度的指定

    typedef struct Point{ unsigned short x; unsigned short y; }mPoint;//点坐标 typedef struct Line{ mPoint ...

  8. 菜鸟学习-C语言函数参数传递详解-结构体与数组 分类: C/C++ Nginx 2015-07-14 10:24 89人阅读 评论(0) 收藏

    C语言中结构体作为函数参数,有两种方式:传值和传址. 1.传值时结构体参数会被拷贝一份,在函数体内修改结构体参数成员的值实际上是修改调用参数的一个临时拷贝的成员的值,这不会影响到调用参数.在这种情况下 ...

  9. C语言:结构体与数组

    #include <stdio.h> struct book{ ]; ]; int price; }; ] = {,,,,,,,,,}; int main(){ struct book * ...

随机推荐

  1. CentOS下用Tomcat+Zookeeper+Nginx+Solr完美搭建SolrCloud平台(五)

    六.修改 /etc/rc.d/rc.local 文件,设置开机自启动 1.nginx 主机的设置 [root@nginx 桌面]# vi /etc/rc.d/rc.local #!/bin/sh to ...

  2. Windows 环境下基于 Redis 的 Celery 任务调度模块的实现

    搭建环境: Windows-x64 10 Celery 3.1.23 Celery-with-redis 3.0 Redis-win32-win64 2.4.5   实现步骤: 1.安装 Redis ...

  3. SVN的版本日期

    SVN还可以使用版本日期来指定某个版本,日期格式使用ISO-8601标准,一般是yyyy-mm-dd hh:mm:ss.当你指定一个日期,SVN会在版本库中找到最接近这个日期的版本. SVN对日期的解 ...

  4. Android 设置横屏或竖屏

    方法一:在AndroidManifest.xml中配置 如果不想让软件在横竖屏之间切换,最简单的办法就是在项目的AndroidManifest.xml中找到你所指定的activity中加上androi ...

  5. android.annotation cannot be resolved

    http://stackoverflow.com/questions/14870596/android-annotation-cannot-be-resolved As Artem suggested ...

  6. Linux-PAM(Linux下的密碼認證和安全机制)系統管理員指南(中文版)

    he Linux-PAM 系统管理员指南作者:Andrew G. Morgan, morgan@linux.kernel.org翻译:孙国清(Thomas Sun),thomassun@yeah.ne ...

  7. (源)V8 Engine 编译

    v8 engine编译 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !imp ...

  8. PHP之open_ssl

    http://www.wapm.cn/phpdoc/zh/openssl.installation.html http://liuxufei.com/weblog/jishu/376.html dem ...

  9. C#_Ajax_分页

    using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace MvcTe ...

  10. Android(java)学习笔记128:使用proguard混淆android代码

    1.当前是有些工具比如apktool,dextojar等是可以对我们android安装包进行反编译,获得源码的.为了减少被别人破解,导致源码泄露,程序被别人盗取代码,等等.我们需要对代码进行混淆,an ...