博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[C++][IO]读写二进制文件
阅读量:6934 次
发布时间:2019-06-27

本文共 744 字,大约阅读时间需要 2 分钟。

1. 以二进制方式读写结构体

struct 
Student
{
    
string name;
    
string sex;
    
int 
age;
}
 
void 
write(string filePath,
const 
struct 
Student* stu,
int 
n)
{
    
FILE 
*fp;
    
int 
i;
    
if
((fp=
fopen
(filePath,
"wb"
))==NULL)
    
{
        
printf
(
"cant open the file"
);
        
return
;
    
}
    
for
(i=0;i<n;i++)
    
{
        
if
(
fwrite
(&stu[i],
sizeof
(
struct 
Student),1,fp)!=1)
        
printf
(
"file write error\n"
);
    
}
    
fclose
(fp);
}
 
void 
read(string filePath,
const 
struct 
Student* stu,
int 
n)
{
    
FILE 
*fp;
    
int 
i;
    
if
((fp=
fopen
(filePath,
"rb"
))==NULL)
    
{
        
printf
(
"cant open the file"
);
        
return
;
    
}
    
for
(i=0;i<n;i++)
    
{
        
if
(
fread
(&stu[i],
sizeof
(
struct 
Student),1,fp)!=1)
        
printf
(
"file read error\n"
);
    
}
    
fclose
(fp);
}
本文转自静默虚空博客园博客,原文链接:http://www.cnblogs.com/jingmoxukong/articles/2132600.html,如需转载请自行联系原作者
你可能感兴趣的文章
Flash中文字体嵌入终极解决方案
查看>>
我的友情链接
查看>>
linux,ssh,ftp,server
查看>>
安全与加密-使用gpg和openssl实现加密与解密
查看>>
Authentication method 'caching_sha2_password' not supported by any of the available plugins.
查看>>
从库备份中恢复一张表
查看>>
防止被压力测试
查看>>
[Eclipse] - Eclipse空格替换tab
查看>>
使用XML模板在excel进行配置
查看>>
组策略的优先级是 本地》站点》域》组织单位
查看>>
构造函数,析构函数,对象连的简单应用
查看>>
巧用rsyslog收集多套日志并做单套日志的过滤分离
查看>>
UI高级----Images.xcassets
查看>>
linux select与多进程的结合
查看>>
Can't open /var/run/atd.pid to signal atd. No atd running?
查看>>
WebSphere概要文件类型
查看>>
app打开本系统自动登陆设计
查看>>
Oracle XTTS跨平台数据库迁移(从Unix迁移数据库到Linux)_Oracle数据库迁移项
查看>>
Linux服务器后门检测RKHunter及被***后处理思路
查看>>
Python - while语句和if语句 的 用法 及 代码
查看>>