使用ofstream crash C ++编写数据

时间:2013-11-23 19:20:08

标签: c++ ofstream char-pointer

我正在使用的IDE(visual studio 2012)没有显示任何错误或警告,但我基本上试图将二进制数据写入名为wizdata.bin的文件中,并且我已经声明了我的向导的一些对象类。 Wizard类具有很少的函数,包含4个成员变量(1个字符串和3个int)。当我在Wizard类中调用名为“save”的函数时,控制台程序停止工作,但它确实设法编写了一个String类成员变量名称。之后它无法在类中编写任何一个int。所以问题是我在尝试将int写入二进制文件时遇到了什么问题?在Wizard.cpp中受到赞扬的部分导致了崩溃。

继承我的代码文件:

Wizard.h:

    #ifndef WIZARD_H
    #define WIZARD_H

    #include <string>
    #include <fstream>

    class Wizard
    {
    public:
        Wizard();
        Wizard( std::string name, int hp, int mp, int armor );

        void print();

        void save( std::ofstream& outFile );
        void load( std::ifstream& inFile );

    private:
        std::string name;
        int hitPoints;
        int magicPoints;
        int armor;
    };

    #endif

Wizard.cpp:

    #include "Wizard.h"
    #include <iostream>
    using namespace std;

    Wizard::Wizard()
    {
        name = "Default";
        hitPoints = 0;
        magicPoints = 0;
        armor = 0;
    }

    Wizard::Wizard( string name, int hp, int mp, int armor )
    {
        this->name = name;
        hitPoints = hp;
        magicPoints = mp;
        this->armor = armor;
    }

    void Wizard::print()
    {
        cout << "Name= " << name << endl;
        cout << "HP= " << hitPoints << endl;
        cout << "MP= " << magicPoints << endl;
        cout << "Armor= " << armor << endl;
        cout << endl;
    }

    void Wizard::save( ofstream& outFile )
    {
        outFile.write( name.c_str(), name.size() );
        //outFile.write( (char*)hitPoints, sizeof(hitPoints) );
        //outFile.write( (char*)magicPoints, sizeof(magicPoints) );
        //outFile.write( (char*)armor, sizeof(armor) );
    }

    void Wizard::load( ifstream& inFile )
    {
        inFile.read( (char*)name.c_str(), name.size() );
        inFile.read( (char*)&hitPoints, sizeof(hitPoints) );
        inFile.read( (char*)&magicPoints, sizeof(magicPoints) );
        inFile.read( (char*)&armor, sizeof(armor) );
    }

和Main.cpp:

    #include "Wizard.h"
    using namespace std;

    int main()
    {
        Wizard wiz0( "Gandalf", 25, 100, 10 );
        Wizard wiz1( "Loki", 50, 150, 12 );
        Wizard wiz2( "Magius", 10, 75, 6 );

        ofstream outFile;
        outFile.open( "wizdata.bin", ios_base::binary );

        if( outFile )
        {
            wiz0.save( outFile );
            wiz1.save( outFile );
            wiz2.save( outFile );

            outFile.close();
        }

        return 0;
    }

这是我加载数据的第二个控制台应用程序的“Main.cpp”:

    #include "Wizard.h"
    #include <iostream>
    using namespace std;

    int main()
    {
        Wizard wiz0;
        Wizard wiz1;
        Wizard wiz2;

        cout << "BEFORE LOADING..." << endl;
        wiz0.print();
        wiz1.print();
        wiz2.print();

        ifstream inFile;
        inFile.open( "wizdata.bin", ios_base::binary );

        if( inFile )
        {
            wiz0.load( inFile );
            wiz1.load( inFile );
            wiz2.load( inFile );

            inFile.close();
        }

        cout << "AFTER LOADING..." << endl;
        wiz0.print();
        wiz1.print();
        wiz2.print();

        return 0;
    }

加载数据后,这些是值: 姓名:甘道夫 惠普:25 MP:100 护甲:10

姓名:Loki2 惠普:38400 议员:3072 护甲:1734429952

姓名:ius 惠普:75 议员:6 护甲:0

2 个答案:

答案 0 :(得分:1)

简短回答:

使用移位运算符'&lt;&lt;'而不是写()。

稍微长一点回答:

问题在于演员。 (char *)数字将数字解释为指针,当然不能指向任何有意义的数字。

答案 1 :(得分:0)

write的第一个参数应该是指向要写入的数据的指针。所以试试这个:

outFile.write( (char*)&hitPoints, sizeof(hitPoints) );