簡體   English   中英

使用ofstream crash C ++寫入數據

[英]Writing data with ofstream crash C++

我正在使用的IDE(Visual Studio 2012)沒有顯示任何錯誤或警告,但我基本上是在嘗試將二進制數據寫入名為wizdata.bin的文件中,並且我聲明了Wizard類的一些對象。 該向導類具有幾個具有4個成員變量(1個字符串和3個整數)的函數。 當我在向導類中調用名為“保存”的函數時,控制台程序停止工作,但是它確實設法寫入了向導類成員變量名稱,即字符串。 之后,它將無法寫入該類中的任何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;
    }

加載數據后,這些值為以下值:名稱:Gandalf HP:25 MP:100護甲:10

名稱:Loki2生命值:38400 MP:3072護甲:1734429952

名稱:ius HP:75 MP:6護甲:0

簡短答案:

使用移位運算符“ <<”而不是write()。

一點點更長的答案:

問題是演員。 (char *)number將數字解釋為指針,它當然不能指向任何有意義的東西。

write的第一個參數應該是指向要寫入的數據的指針。 所以試試這個:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM