ページ 11

クラス内でのファイル入出力について

Posted: 2012年8月04日(土) 10:20
by AliceCoder
ファイルへの書き込みができていない現象がおきました。
クラスのメンバでFILE *file;を宣言し、そのクラスのコンストラクタでfile=fopen("aaa.txt","w");としました。ポインタのNULLチェックもしておりそこまでは問題ないはずです。デストラクタでfilecloseしております。
メンバ関数内でfprintfを使っても実際にファイルに書き込まれていませんでした。指定した名前どおりのファイルはちゃんと作られていました。

自分で試しにクラスを使わずにファイル出力だけのプログラムも作って実行しましたが、それが上手くできていたのでクラスを使用したことで何か起きているのかな?と自分では思います。
情報が少ないかもですが、何が原因か考えられることをどなたか教えてください。
使っているOSはwindows7 64bit,使用ソフトVC++,言語C++です

Re: クラス内でのファイル入出力について

Posted: 2012年8月04日(土) 10:24
by エクレ
コード貼って下さい

Re: クラス内でのファイル入出力について

Posted: 2012年8月04日(土) 10:37
by AliceCoder
すいません。コードのせるの恥ずかしかったです。
たぶんどこか誤字とかしてそうな雰囲気なので全部コード載せますね

ヘッダ

コード:

#pragma once
#include "Chara.h"
#include "Enemy.h"
#include <stdio.h>
//各種キー***************************
#define UP_KEY (getch3(VK_UP)==1)
#define RIGHT_KEY (getch3(VK_RIGHT)==1)
#define DOWN_KEY (getch3(VK_DOWN)==1)
#define LEFT_KEY (getch3(VK_LEFT)==1)
#define A_KEY (getch3('A')==1)
#define S_KEY (getch3('S')==1)
//***********************************

#define DRAW_PX ((PX-1)*TIPW+(TIPW/2))//プレイヤー表示左上座標
#define DRAW_PY (PY*TIPH)



class Player :
	public Chara
{
public:
	Player(void);
	~Player(void);
	void draw(void);
	void act(void);
protected:
	//player絵構成**********
	struct KUTI{
		int tp_x,tp_y;//くちばしの先座標
		int naka_x,naka_y;//くちばしの真ん中に伸ばすラインの始点
		int up_x,up_y;
		int down_x,down_y;
	};
	KUTI kuti[8];
	int kuti_w;//くちばしの広さ
	int head_w;//頭のでかさ(円の半径)
	//**********************

	void atack_start(void);
	void damaged_start(void);
	//状態に応じて実行する用の関数*****************************************************
	void cflg_stop(void);
	void cflg_move(void);
	void cflg_damaged(void);
	void cflg_atack(void);
	//*********************************************************************************

	//方向にあわせたdraw関数
	void up_draw(void);
	void rightup_draw(void);
	void right_draw(void);
	void rightdown_draw(void);
	void down_draw(void);
	void leftdown_draw(void);
	void left_draw(void);
	void leftup_draw(void);
	//**********************
	Enemy* enemys;//敵

	//デバッグ用
	FILE *file;
};
cpp

コード:

 
#include "Player.h"

#include <math.h>
#define RAD(k) ((k)*3.141592653589790/180.0)

Player::Player(void)
{
	//player絵初期化**********
	kuti_w = 32;
	head_w = 32;
	for(int i = UP;i <= LEFTUP;i++){//上から右回りに
		kuti[i].naka_x = cos( RAD(i*45-90) ) * kuti_w;
		kuti[i].naka_y = sin( RAD(i*45-90) ) * kuti_w;
		kuti[i].tp_x = cos( RAD(i*45-90) ) * (kuti_w * 1.7);
		kuti[i].tp_y = sin( RAD(i*45-90) ) * (kuti_w * 1.7);
		kuti[i].up_x = cos( RAD(i*45-90-kuti_w) ) * kuti_w;
		kuti[i].up_y = sin( RAD(i*45-90-kuti_w) ) * kuti_w;
		kuti[i].down_x = cos( RAD(i*45-90+kuti_w) ) * kuti_w;
		kuti[i].down_y = sin( RAD(i*45-90+kuti_w) ) * kuti_w;
	}
	//**********************

	y = 3;
	x = 5;
	h = 64;
	w = 64;
	//デバッグ用
	file = fopen("sokudo.txt","w");
	if(file==NULL){
		x=100;
	}
        fprintf(file,"sokudo:%02d,vv:%02d\n",sokudo,vv);
	return;
}


Player::~Player(void)
{
	//デバッグ用
	fclose(file);
	return;
}

void Player::draw(void){

	(this->*draw_p[cmuki])();//プレイヤー表示
	
}

void Player::act(void)
{
	(this->*cflg_act_p[cflg])();//状態にあわせた処理
	return;
}

void Player::atack_start(void)
{
	cflg = ATACK;
	sokudo = 0;
	vv = SYOKASOKU;
	return;
}

void Player::damaged_start(void)
{
	cflg = DAMAGED;
	sokudo = 0;
	vv = -SYOKASOKU;
	return;
}
//方向にあわせたdraw関数*************************************************************
void Player::up_draw(void)
{
	//player表示
	dg_circle(bb,DRAW_PX+w/2,DRAW_PY+h/2-sokudo,h/2,RGB(255,255,0));
	dg_line(bb,DRAW_PX+w/2 + kuti[cmuki].naka_x,	DRAW_PY+h/2 + kuti[cmuki].naka_y - sokudo,	DRAW_PX+w/2 + kuti[cmuki].tp_x,	DRAW_PY+h/2 + kuti[cmuki].tp_y - sokudo,	RGB(255,255,255));
	dg_line(bb,DRAW_PX+w/2 + kuti[cmuki].up_x,		DRAW_PY+h/2 + kuti[cmuki].up_y - sokudo,	DRAW_PX+w/2 + kuti[cmuki].tp_x,	DRAW_PY+h/2 + kuti[cmuki].tp_y - sokudo,	RGB(255,255,255));
	dg_line(bb,DRAW_PX+w/2 + kuti[cmuki].down_x,	DRAW_PY+h/2 + kuti[cmuki].down_y - sokudo,	DRAW_PX+w/2 + kuti[cmuki].tp_x,	DRAW_PY+h/2 + kuti[cmuki].tp_y - sokudo,	RGB(255,255,255));
	return;
}
void Player::rightup_draw(void)
{
	//player表示
	dg_circle(bb,DRAW_PX+w/2+sokudo,	DRAW_PY+h/2-sokudo,	h/2,	RGB(255,255,0));
	dg_line(bb,DRAW_PX+w/2 + kuti[cmuki].naka_x + sokudo,	DRAW_PY+h/2 + kuti[cmuki].naka_y - sokudo,	DRAW_PX+w/2 + kuti[cmuki].tp_x+sokudo,	DRAW_PY+h/2 + kuti[cmuki].tp_y - sokudo,	RGB(255,255,255));
	dg_line(bb,DRAW_PX+w/2 + kuti[cmuki].up_x + sokudo,		DRAW_PY+h/2 + kuti[cmuki].up_y - sokudo,	DRAW_PX+w/2 + kuti[cmuki].tp_x+sokudo,	DRAW_PY+h/2 + kuti[cmuki].tp_y - sokudo,	RGB(255,255,255));
	dg_line(bb,DRAW_PX+w/2 + kuti[cmuki].down_x + sokudo,	DRAW_PY+h/2 + kuti[cmuki].down_y - sokudo,	DRAW_PX+w/2 + kuti[cmuki].tp_x+sokudo,	DRAW_PY+h/2 + kuti[cmuki].tp_y - sokudo,	RGB(255,255,255));
	return;
}
void Player::right_draw(void)
{
	//player表示
	dg_circle(bb,DRAW_PX+w/2+sokudo,DRAW_PY+h/2,h/2,RGB(255,255,0));
	dg_line(bb,DRAW_PX+w/2 + kuti[cmuki].naka_x + sokudo,	DRAW_PY+h/2 + kuti[cmuki].naka_y,	DRAW_PX+w/2 + kuti[cmuki].tp_x+sokudo,	DRAW_PY+h/2 + kuti[cmuki].tp_y,	RGB(255,255,255));
	dg_line(bb,DRAW_PX+w/2 + kuti[cmuki].up_x + sokudo,		DRAW_PY+h/2 + kuti[cmuki].up_y,		DRAW_PX+w/2 + kuti[cmuki].tp_x+sokudo,	DRAW_PY+h/2 + kuti[cmuki].tp_y,	RGB(255,255,255));
	dg_line(bb,DRAW_PX+w/2 + kuti[cmuki].down_x + sokudo,	DRAW_PY+h/2 + kuti[cmuki].down_y,	DRAW_PX+w/2 + kuti[cmuki].tp_x+sokudo,	DRAW_PY+h/2 + kuti[cmuki].tp_y,	RGB(255,255,255));
	return;
}
void Player::rightdown_draw(void)
{
	//player表示
	dg_circle(bb,DRAW_PX+w/2+sokudo,DRAW_PY+h/2+sokudo,h/2,RGB(255,255,0));
	dg_line(bb,DRAW_PX+w/2 + kuti[cmuki].naka_x + sokudo,	DRAW_PY+h/2 + kuti[cmuki].naka_y + sokudo,	DRAW_PX+w/2 + kuti[cmuki].tp_x+sokudo,	DRAW_PY+h/2 + kuti[cmuki].tp_y + sokudo,	RGB(255,255,255));
	dg_line(bb,DRAW_PX+w/2 + kuti[cmuki].up_x + sokudo,		DRAW_PY+h/2 + kuti[cmuki].up_y + sokudo,	DRAW_PX+w/2 + kuti[cmuki].tp_x+sokudo,	DRAW_PY+h/2 + kuti[cmuki].tp_y + sokudo,	RGB(255,255,255));
	dg_line(bb,DRAW_PX+w/2 + kuti[cmuki].down_x + sokudo,	DRAW_PY+h/2 + kuti[cmuki].down_y + sokudo,	DRAW_PX+w/2 + kuti[cmuki].tp_x+sokudo,	DRAW_PY+h/2 + kuti[cmuki].tp_y + sokudo,	RGB(255,255,255));
	return;
}
void Player::down_draw(void)
{
	//player表示
	dg_circle(bb,DRAW_PX+w/2,DRAW_PY+h/2+sokudo,h/2,RGB(255,255,0));
	dg_line(bb,DRAW_PX+w/2 + kuti[cmuki].naka_x,	DRAW_PY+h/2 + kuti[cmuki].naka_y + sokudo,	DRAW_PX+w/2 + kuti[cmuki].tp_x,	DRAW_PY+h/2 + kuti[cmuki].tp_y + sokudo,	RGB(255,255,255));
	dg_line(bb,DRAW_PX+w/2 + kuti[cmuki].up_x,		DRAW_PY+h/2 + kuti[cmuki].up_y + sokudo,	DRAW_PX+w/2 + kuti[cmuki].tp_x,	DRAW_PY+h/2 + kuti[cmuki].tp_y + sokudo,	RGB(255,255,255));
	dg_line(bb,DRAW_PX+w/2 + kuti[cmuki].down_x ,	DRAW_PY+h/2 + kuti[cmuki].down_y + sokudo,	DRAW_PX+w/2 + kuti[cmuki].tp_x,	DRAW_PY+h/2 + kuti[cmuki].tp_y + sokudo,	RGB(255,255,255));
	return;
}
void Player::leftdown_draw(void)
{
	//player表示
	dg_circle(bb,DRAW_PX+w/2-sokudo,DRAW_PY+h/2+sokudo,h/2,RGB(255,255,0));
	dg_line(bb,DRAW_PX+w/2 + kuti[cmuki].naka_x - sokudo,	DRAW_PY+h/2 + kuti[cmuki].naka_y + sokudo,	DRAW_PX+w/2 + kuti[cmuki].tp_x-sokudo,	DRAW_PY+h/2 + kuti[cmuki].tp_y + sokudo,	RGB(255,255,255));
	dg_line(bb,DRAW_PX+w/2 + kuti[cmuki].up_x - sokudo,		DRAW_PY+h/2 + kuti[cmuki].up_y + sokudo,	DRAW_PX+w/2 + kuti[cmuki].tp_x-sokudo,	DRAW_PY+h/2 + kuti[cmuki].tp_y + sokudo,	RGB(255,255,255));
	dg_line(bb,DRAW_PX+w/2 + kuti[cmuki].down_x - sokudo,	DRAW_PY+h/2 + kuti[cmuki].down_y + sokudo,	DRAW_PX+w/2 + kuti[cmuki].tp_x-sokudo,	DRAW_PY+h/2 + kuti[cmuki].tp_y + sokudo,	RGB(255,255,255));
	return;
}
void Player::left_draw(void)
{
	//player表示
	dg_circle(bb,DRAW_PX+w/2-sokudo,DRAW_PY+h/2,h/2,RGB(255,255,0));
	dg_line(bb,DRAW_PX+w/2 + kuti[cmuki].naka_x - sokudo,	DRAW_PY+h/2 + kuti[cmuki].naka_y,	DRAW_PX+w/2 + kuti[cmuki].tp_x-sokudo,	DRAW_PY+h/2 + kuti[cmuki].tp_y,	RGB(255,255,255));
	dg_line(bb,DRAW_PX+w/2 + kuti[cmuki].up_x - sokudo,		DRAW_PY+h/2 + kuti[cmuki].up_y,	DRAW_PX+w/2 + kuti[cmuki].tp_x-sokudo,	DRAW_PY+h/2 + kuti[cmuki].tp_y,		RGB(255,255,255));
	dg_line(bb,DRAW_PX+w/2 + kuti[cmuki].down_x - sokudo,	DRAW_PY+h/2 + kuti[cmuki].down_y,	DRAW_PX+w/2 + kuti[cmuki].tp_x-sokudo,	DRAW_PY+h/2 + kuti[cmuki].tp_y,	RGB(255,255,255));
	return;
}
void Player::leftup_draw(void)
{
	//player表示
	dg_circle(bb,DRAW_PX+w/2-sokudo,DRAW_PY+h/2-sokudo,h/2,RGB(255,255,0));
	dg_line(bb,DRAW_PX+w/2 + kuti[cmuki].naka_x - sokudo,	DRAW_PY+h/2 + kuti[cmuki].naka_y - sokudo,	DRAW_PX+w/2 + kuti[cmuki].tp_x-sokudo,	DRAW_PY+h/2 + kuti[cmuki].tp_y - sokudo,	RGB(255,255,255));
	dg_line(bb,DRAW_PX+w/2 + kuti[cmuki].up_x - sokudo,		DRAW_PY+h/2 + kuti[cmuki].up_y - sokudo,	DRAW_PX+w/2 + kuti[cmuki].tp_x-sokudo,	DRAW_PY+h/2 + kuti[cmuki].tp_y - sokudo,	RGB(255,255,255));
	dg_line(bb,DRAW_PX+w/2 + kuti[cmuki].down_x - sokudo,	DRAW_PY+h/2 + kuti[cmuki].down_y - sokudo,	DRAW_PX+w/2 + kuti[cmuki].tp_x-sokudo,	DRAW_PY+h/2 + kuti[cmuki].tp_y - sokudo,	RGB(255,255,255));
	return;
}
/*************************************************************************************/

//各状態にあわせたcflg_関数**********************************************************
void Player::cflg_stop(void)
{
	if( can_moved && cflg == STOP){//プレイヤーが入力を受け付けているかどうか
		if( A_KEY ){
			if( RIGHT_KEY && UP_KEY ){
				cmuki = RIGHTUP;
				if( Chara::pmap->can_move(x + 1,y - 1 )){//移動先の座標のフィールドが移動可能な場所かどうか
					Chara::pmap->scr_rightup();
					move_rightup();
				}
			}else if(  RIGHT_KEY && DOWN_KEY){
				cmuki = RIGHTDOWN;
				if( Chara::pmap->can_move(x + 1,y + 1 )){//移動先の座標のフィールドが移動可能な場所かどうか
					Chara::pmap->scr_rightdown();
					move_rightdown();
				}
			}else if( LEFT_KEY && UP_KEY ){
				cmuki = LEFTUP;
				if( Chara::pmap->can_move(x - 1,y - 1 )){//移動先の座標のフィールドが移動可能な場所かどうか
					Chara::pmap->scr_leftup();
					move_leftup();
				}
			}else if( LEFT_KEY && DOWN_KEY ){
				cmuki = LEFTDOWN;
				if( Chara::pmap->can_move(x - 1,y + 1 )){//移動先の座標のフィールドが移動可能な場所かどうか
					Chara::pmap->scr_leftdown();
					move_leftdown();
				}
			}
		}
		else{
			if( S_KEY ){//攻撃コマンド
				atack_start();
			}else if( UP_KEY ){
				cmuki = UP;
				if( Chara::pmap->can_move(x,y - 1 )){//移動先の座標のフィールドが移動可能な場所かどうか
					Chara::pmap->scr_up();//マップをスクロール
					move_up();
				}
			}else if( DOWN_KEY ){
				cmuki = DOWN;
				if( Chara::pmap->can_move(get_x(),get_y() + 1) ){
					Chara::pmap->scr_down();
					move_down();
				}
			}else if( LEFT_KEY ){
				cmuki = LEFT;
				if( Chara::pmap->can_move(get_x() - 1,get_y()) ){
					Chara::pmap->scr_left();
					move_left();
				}
			}else if( RIGHT_KEY ){
				cmuki = RIGHT;
				if( Chara::pmap->can_move(get_x() + 1,get_y()) ){
					Chara::pmap->scr_right();
					move_right();
				}
			}
		}
	}
	return;
}
void Player::cflg_move(void)
{
	//カメラ移動****************************************
	switch(cmuki){
	case UP:
		camera_y += 2;
		break;
	case RIGHTUP:
		camera_y += 2;
		camera_x -= 2;
		break;
	case RIGHT:
		camera_x -= 2;
		break;
	case RIGHTDOWN:
		camera_y -= 2;
		camera_x -= 2;
		break;
	case DOWN:
		camera_y -= 2;
		break;
	case LEFTDOWN:
		camera_y -= 2;
		camera_x += 2;
		break;
	case LEFT:
		camera_x += 2;
		break;
	case LEFTUP:
		camera_y += 2;
		camera_x += 2;
		break;
	}

	//******************************************
	if( (camera_x)%64==0 && camera_y%64==0 && cflg==MOVE){
		cflg = STOP;
		can_moved = false;//行動終了
	}
	return;
}
void Player::cflg_damaged(void)
{
	return;
}
void Player::cflg_atack(void)
{
	fprintf(file,"sokudo:%02d,vv:%02d\n",sokudo,vv);
	//攻撃モーション******************
	sokudo -= vv;
	vv++;
	if(sokudo<0){
		sokudo=0;
		vv = 1;
		cflg = STOP;
		can_moved = false;//行動終了
	}
	//********************************
	return;
}
//***********************************************************************************

Re: クラス内でのファイル入出力について

Posted: 2012年8月04日(土) 10:48
by エクレ
fprintfの戻り値はどうなっていますか?

Re: クラス内でのファイル入出力について

Posted: 2012年8月04日(土) 11:19
by AliceCoder
戻り値は17でした。
これはマイナスの値じゃないから正常に書き込まれたってことですよね。
どこかでファイルのデータ消してしまっているってことですかね。

Re: クラス内でのファイル入出力について

Posted: 2012年8月04日(土) 11:30
by エクレ
戻り値が17ならちゃんと書き込まれてそうですね
でもそれでもファイルに書き込まれていないとなると、ちょっと分からないですね。

Playerクラスを解放し忘れてfcloseが実行されてなかった、とかではないですよね?
ためしにfprintfの後にすぐfcloseを実行してみてどうなるかを見てみるとか。

Re: クラス内でのファイル入出力について

Posted: 2012年8月04日(土) 11:47
by AliceCoder
書き込み成功しました!解放をしていなかったのが原因でした。
fcloseをしていなかったらファイル書き込みが無効になるってことですかね。
これは自分ひとりでは気が付けなかったです。
エクレさんありがとうございました。