| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 541 人关注过本帖
标题:二进制文件数据的读取和写入出错
只看楼主 加入收藏
ehszt
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:40
帖 子:1745
专家分:3216
注 册:2015-12-2
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:6 
二进制文件数据的读取和写入出错
/***********************************
二进制文件数据的读取和写入
***********************************/
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    int age,height,readAge,readHeight;
    cout<<"please input age and height:";
    cin>>age>>height;

    ofstream outBinaryFile("binary.abc",ios::binary|ios::app);
    if(outBinaryFile.fail())
    {
        cout<<"open file failed."<<endl;
        exit(0);
    }
    outBinaryFile.write(reinterpret_cast<char *>(&age),sizeof(age));
    outBinaryFile.write(reinterpret_cast<char *>(&height),sizeof(height));
    outBinaryFile.close();

    ifstream inBinaryFile("binary.abc",ios::binary);
    if(inBinaryFile.fail())
    {
        cout<<"open file failed."<<endl;
        exit(0);
    }
    inBinaryFile.seekg(-sizeof(int)*2,ios::end); //每次读文件,从文件末尾前移两个int字节,不知道对不对,请各位指点
    inBinaryFile.read(reinterpret_cast<char *>(&readAge),sizeof(readAge));
    inBinaryFile.read(reinterpret_cast<char *>(&readHeight),sizeof(readHeight));
    inBinaryFile.close();

    cout<<"age from file:"<<readAge<<endl
       <<"height from file:"<<readHeight<<endl;

    return 0;
}
输出的数值不对
搜索更多相关主题的帖子: age sizeof char 二进制 文件 
2023-10-05 12:45
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:20 
输出的数值不对
你得说说怎么个不对,即“不对”的现象是什么?

去除无关代码
程序代码:
#include <iostream>
#include <fstream>
using namespace std;

int main( void )
{
    {
        std::ofstream file( "binary.abc", ios::binary|ios::app );
        if( file )
        {
            int a=1, b=2;
            file.write( reinterpret_cast<char*>(&a), sizeof(a) );
            file.write( reinterpret_cast<char*>(&b), sizeof(b) );
        }
    }

    {
        std::ifstream file( "binary.abc", ios::binary|ios::app );
        if( file )
        {
            int a, b;
            std::ifstream::traits_type::off_type off = sizeof(a) + sizeof(b);
            file.seekg( -off,ios::end );
            file.read( reinterpret_cast<char*>(&a), sizeof(a) );
            file.read( reinterpret_cast<char*>(&b), sizeof(b) );
            cout << a << '\n' << b << endl;
        }
    }
}
你编译运行一下,把你认为不对的输出贴出来。
2023-10-05 14:47
ehszt
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:40
帖 子:1745
专家分:3216
注 册:2015-12-2
收藏
得分:0 
图片附件: 游客没有浏览图片的权限,请 登录注册


2023-10-05 14:51
ehszt
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:40
帖 子:1745
专家分:3216
注 册:2015-12-2
收藏
得分:0 
应该输出19 110才对
2023-10-05 14:52
ehszt
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:40
帖 子:1745
专家分:3216
注 册:2015-12-2
收藏
得分:0 
std::ifstream::traits_type::off_type 这个是什么意思?
直接这么写有什么错误?    inBinaryFile.seekg(-sizeof(int)*2,ios::end);

[此贴子已经被作者于2023-10-5 15:06编辑过]

2023-10-05 15:02
ehszt
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:40
帖 子:1745
专家分:3216
注 册:2015-12-2
收藏
得分:0 
图片附件: 游客没有浏览图片的权限,请 登录注册

发现这样转一下结果就是对的,直接用右边的去替换a就不对!
2023-10-05 15:14
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:0 
以 uint8_t 转 int16_t 为例

uint8_t a = -1; // a的值为 255
int16_t b = a; // b的值为 255

2023-10-05 16:11
快速回复:二进制文件数据的读取和写入出错
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015374 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved