| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1107 人关注过本帖
标题:不懂就问,编译过了,为啥结果输不出呢
只看楼主 加入收藏
CCCCW
Rank: 2
等 级:论坛游民
帖 子:8
专家分:27
注 册:2022-2-26
结帖率:66.67%
收藏
已结贴  问题点数:10 回复次数:2 
不懂就问,编译过了,为啥结果输不出呢

#include<iostream>
#include<iomanip>
#include<fstream>
#include<string>
#define _CRT_SECURE_NO_WARNINGS
using namespace std;
class CStudent
{
public:
    CStudent(const char* name,const char* id, float score = 0);
    void print();//输出
    friend ostream& operator<<(ostream& os, CStudent& stu);
    friend istream& operator>>(istream& is, CStudent& stu);
private:
    char strName[10];
    char strID[10];
    float fScore;
};
CStudent::CStudent(const char* name,const char* id,float score)
{
    strncpy_s(strName, name, strlen(strName)+1);
    strncpy_s(strID, id,strlen(strID)+1);
    fScore = score;
}
void CStudent::print()
{
    cout << endl << "学生信息如下:" << endl;
    cout << "姓名:" << strName << endl;
    cout << "学号:" << strID << endl;
    cout << "成绩:" << fScore << endl;
}
std::ostream& operator<<(ostream& os, CStudent& stu)
{
    //使用定长格式,使每一个类的数据长度为24个字节
    os.write(stu.strName, 10);
    os.write(stu.strID, 10);
    os.write((char*)& stu.fScore,4);//使float数据类型占4字节
        return os;
}
std::istream& operator>>(istream& is, CStudent& stu)
{
    char name[10], id[10];
    is.read(name, 10);
    is.read(id, 10);
    is.read((char*)&stu.fScore, 4);//读入4字节数据作为float类型
    strncpy_s(stu.strName, name, strlen(stu.strName)+1);
    strncpy_s(stu.strID, id, strlen(stu.strID)+1);
    return is;
}
void main()
{
    CStudent stu1("MaWenTao", "99001", 88);
    CStudent stu2("LiMing", "99002", 92);
    CStudent stu3("WangFang", "99003", 89);
    CStudent stu4("YangYang", "99004", 90);
    CStudent stu5("DingNing", "99005", 80);
    fstream file1;
    file1.open("student.dat", ios::out | ios::in | ios::binary);
    file1 << stu1 << stu2 << stu3 << stu4 << stu5;
    CStudent* one = new CStudent("","");
    const int size = 24;//每一个类的数据长度为24字节,与前呼应
    file1.seekp(size * 4);  file1 >> *one;  one->print();
    file1.seekp(size * 1);  file1 >> *one;  one->print();
    file1.seekp(size * 2, ios::cur);  file1 >> *one;  one->print();
    file1.close();
    delete one;
}
图片附件: 游客没有浏览图片的权限,请 登录注册
搜索更多相关主题的帖子: print stu const name char 
2022-04-17 21:39
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:10 
看不懂你那一系列 file1.seekp 是想实现什么功能,而你又什么都不肯说,连题目都不给。

程序代码:
#define _CRT_SECURE_NO_WARNINGS // 进VC需要此宏

#include <iostream>
#include <fstream>
#include <type_traits>
using namespace std;

class CStudent
{
public:
    CStudent() = default;
    CStudent( const char* name, const char* id, float score=0.0f );
    friend std::ostream& operator<<( std::ostream& os, const CStudent& stu );

private:
    char name_[10];
    char id_[10];
    float score_;
};

CStudent::CStudent( const char* name, const char* id, float score )
{
    strncpy( name_, name, sizeof(name_) );
    name_[sizeof(name_)-1]='\0';

    strncpy( id_, id, sizeof(id_) );
    name_[sizeof(id_)-1]='\0';

    score_ = score;
}

std::ostream& operator<<( std::ostream& os, const CStudent& stu )
{
    os << '\n';
    os << "学生信息如下:\n";
    os << "姓名:" << stu.name_ << '\n';
    os << "学号:" << stu.id_ << '\n';
    os << "成绩:" << stu.score_;
    return os;
}

int main( void )
{
    // 仅当“可平凡复制”的类型,才可以使用std::memcpy复制或以std::ofstream::write()/std::ifstream::read()序列化自/到二进制文件的对象
    static_assert( is_trivially_copyable_v<CStudent> );

    CStudent students[] = { { "MaWenTao", "99001", 88 }
                          , { "LiMing",   "99002", 92 }
                          , { "WangFang", "99003", 89 }
                          , { "YangYang", "99004", 90 }
                          , { "DingNing", "99005", 80 } };

    fstream file( "student.dat", ios_base::in|ios_base::out|ios::binary|ios::trunc );
    if( !file )
    {
        cerr  <<  " 文件创建失败.\n";
        return 1 ;
     }

    if( !file.write((const char*)students,sizeof(students)) )
    {
        cerr  <<  " 文件写入失败.\n";
        return 2 ;
    }

    // 将文件的 输入位置指示器 置于文件开始,进行读取
    file.seekg( 0 );
    for( CStudent stu; file.read((char*)&stu,sizeof(stu)); )
    {
        cout << stu << '\n';
    }
}
2022-04-18 09:14
chenyucheng
Rank: 3Rank: 3
来 自:浙江省台州市临海市
等 级:论坛游侠
威 望:3
帖 子:141
专家分:181
注 册:2022-7-1
收藏
得分:0 
手持两把锟斤拷,口中直呼烫烫烫

爱玩编程的小学生,有Python,但只会一点点C++。

版主->斑竹->竹子,所以版主是竹子。(doge)
//不要让我当版主
2022-08-01 06:42
快速回复:不懂就问,编译过了,为啥结果输不出呢
数据加载中...
 
   



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

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