明解C++中級編を読んだり、ググっても理解できなかったので質問させていただきます。
ゲーム制作で、ifstreamを使ってテキストファイルに書かれた値を読み込もうとしているのですが、反映されません。ご助力お願いできますでしょうか。
やりたいこと
初期位置の変数、"init_X"・"init_Y"にテキストファイルの値を読み込んで、ここではどちらも200を返したい
実際の状況
テキストファイルから読み込もうとしてもどちらの値も0を返してしまう
現在のコードとテキストファイルの状態
C++
1#pragma once 2#include <fstream> 3using namespace std; 4 5class SaveButton; 6class GameObject; 7class EnemyHP_AddButton; 8class EnemyHP_SubButton; 9class X_AddButton; 10class X_SubButton; 11class Y_AddButton; 12class Y_SubButton; 13class ClearScoreButton_Add; 14class ClearScoreButton_Sub; 15class Mouse; 16class MainAdmin 17{ 18protected: 19 //エディターを読み込んだ時の、自機の初期位置 20 int Player_initX; 21 int Player_initY; 22 23 int Player_initHP; 24 int Enemy_initHP; 25 26 int Clear_Score_init; 27 28 char key[256]; 29 bool quitApplication; 30 31 int Player_GraphicHandle; 32 33 std::unique_ptr<GameObject>Player; 34 std::shared_ptr<Mouse>mouse; 35 36 std::unique_ptr<SaveButton>S_button; 37 38 std::unique_ptr<EnemyHP_AddButton>HPAdd; 39 std::unique_ptr<EnemyHP_SubButton>HPSub; 40 41 std::unique_ptr<X_AddButton>X_add; 42 std::unique_ptr<X_SubButton>X_Sub; 43 44 std::unique_ptr<Y_AddButton>Y_add; 45 std::unique_ptr<Y_SubButton>Y_Sub; 46 47 std::unique_ptr <ClearScoreButton_Add>Clear_Add; 48 std::unique_ptr<ClearScoreButton_Sub>Clear_Sub; 49public: 50 51 const char* filename; 52 53 MainAdmin(); 54 ~MainAdmin(); 55 int GetP_initX() { return Player_initX; } 56 int GetP_initY() { 57 return Player_initY; 58 } 59 int GetP_initHP() { 60 return Player_initHP; 61 } 62 int GetE_initHP() { 63 return Enemy_initHP; 64 } 65 int GetClear_Score_Init() 66 { 67 return Clear_Score_init; 68 } 69 std::shared_ptr<Mouse> GetMouse() { return mouse; } 70 void SetClear_Score_Init(int score) { Clear_Score_init = score; } 71 void SetEnemy_HP(int hp) { Enemy_initHP = hp; } 72 void SetinitX(int m_x) { Player_initX = m_x; } 73 void SetinitY(int m_y) { Player_initY = m_y; } 74 char* GetKey() { return key; } 75 void Init(); 76 77 void Update(); 78 79 void SaveFile(); 80 81 void end(); 82 83}; 84 85
mainadmin.cpp
1 2MainAdmin::MainAdmin():filename("savefile.txt"),quitApplication(false) 3{ 4 5 6} 7 8 9 10 11void MainAdmin::Init() 12{ 13 ifstream fileinput; 14 15 16 //fileinput.open("savefile.txt"); 17 fileinput.open(filename); 18 19 //ファイルに書き込みがあったらファイルの値を読み込む 20 21 22 fileinput >> Player_initX >> Player_initY 23 >> Player_initHP >> Enemy_initHP 24 >> Clear_Score_init; 25 26 27 28 fileinput.close(); 29 30} 31 32 33void MainAdmin::SaveFile() 34{ 35 ofstream fileoutput; 36 if (!fileoutput.is_open()) { 37 fileoutput.open(filename); 38 } 39 //ファイルに出力する 40 fileoutput<< 200 << 200 41 << 200 << 200 42 << 200; 43 44 DrawString(320, 240, "セーブしました", GetColor(255, 255, 255)); 45 fileoutput.close(); 46 47 48} 49
savefile.txt 200200200200200
回答1件
あなたの回答
tips
プレビュー