PDA

View Full Version : File Stuff



Lemmy
10-12-2004, 05:37 PM
Hey all,

Im trying to code a simple file writer/reader, but have a problem, for some reason when i try to read the file the first name comes out as * instead of 0. Any help would really be appreciated. I am using visual studio 6, which i think has certain problems when it comes to this sort of stuff. Anyway here is my code.


#ifndef Player_H
#define Player_H

#include <string>
using namespace std;

///////////////////////////////////////////////////////////////////////////////////////////////////////////////
// //
// Structure: The minimum attributes of the player //
// //
///////////////////////////////////////////////////////////////////////////////////////////////////////////////

struct playerMinAttributes{

string name; //Index
int number; //1
int movement; //2
int strength; //3
int magic; //4

};

///////////////////////////////////////////////////////////////////////////////////////////////////////////////
// //
// Class: The bare player object //
// //
///////////////////////////////////////////////////////////////////////////////////////////////////////////////

class barePlayer {

public:

/////////////////////
//default constructor
/////////////////////

barePlayer(string uniqueID="");

////////////
//data entry
////////////

void setUniqueID(string ID);
void setPlayerName(string theName);
void setPlayerMinAttributes(int theInt,int changeIndex);

/////////////
//data access
/////////////

string getUniqueID();
string getPlayerName();
int getPlayerMinAttributes(int changeIndex);


/////////////////////
//protected variables
/////////////////////

protected:

string playerUniqueID;
playerMinAttributes pMinA;
};



#endif

#include "Player.h"


///////////////////////////////////////////////////////////////////////////////////////////////////////////////
// //
// Class: The bare player object //
// //
///////////////////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////
// default constructor
//////////////////////

///////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Method: The bare player //
///////////////////////////////////////////////////////////////////////////////////////////////////////////////

barePlayer::barePlayer(string uniqueID){

playerUniqueID = uniqueID;

}

////////////
//data entry
////////////

///////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Method: Set unique id //
///////////////////////////////////////////////////////////////////////////////////////////////////////////////

void barePlayer::setUniqueID(string uniqueID){

playerUniqueID = uniqueID;

}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Method: Set player name //
///////////////////////////////////////////////////////////////////////////////////////////////////////////////

void barePlayer::setPlayerName(string theName){

pMinA.name = theName;

}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Method: Set player min attributes //
///////////////////////////////////////////////////////////////////////////////////////////////////////////////

void barePlayer::setPlayerMinAttributes(int theInt,int changeIndex){

switch(changeIndex){

case 1:
pMinA.number=theInt;
break;

case 2:
pMinA.movement=theInt;
break;

case 3:
pMinA.strength=theInt;
break;

case 4:
pMinA.magic=theInt;
break;

default:
//cout error
break;

}
}

/////////////
//data access
/////////////

///////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Method: Get unique id //
///////////////////////////////////////////////////////////////////////////////////////////////////////////////

string barePlayer::getUniqueID(){

return playerUniqueID;

}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Method: Get player name //
///////////////////////////////////////////////////////////////////////////////////////////////////////////////

string barePlayer::getPlayerName(){

return pMinA.name;

}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Method: Get player min attributes //
///////////////////////////////////////////////////////////////////////////////////////////////////////////////

int barePlayer::getPlayerMinAttributes(int changeIndex){


switch(changeIndex){

case 1:
return pMinA.number;
break;

case 2:
return pMinA.movement;
break;

case 3:
return pMinA.strength;
break;

case 4:
return pMinA.magic;
break;

default:
return 0;
break;

}

}




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


void writeAPlayer();
void displayPlayers();


fstream file;

barePlayer myPlayer;

int theInt;
string theString;
bool theBool;


void main(){

///////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Tests
///////////////////////////////////////////////////////////////////////////////////////////////////////////////

cin.ignore(256, '\n');
//change for number of players to be inputted
for(int s=0;s<2;s++){

writeAPlayer();

}


displayPlayers();


}

void writeAPlayer(){

file.open("Player.txt", ios::out|ios::app);//open file for appending


///////////////////////////////////////////////////////////////////////////////////////////////////////////////
// //
// The Player Attributes that need to be edited
// //
///////////////////////////////////////////////////////////////////////////////////////////////////////////////


cout << "Player movement : " <<endl;
cin >> theInt;
myPlayer.setPlayerMinAttributes(theInt,2);

cout << "Player strength : " <<endl;
cin >> theInt;
myPlayer.setPlayerMinAttributes(theInt,3);

cout << "Player magic : " <<endl;
std::cin >> theInt;
myPlayer.setPlayerMinAttributes(theInt,4);

///////////////////////////////////////////////////////////////////////////////////////////////////////////////
// //
// Set all other stats to zero
// //
///////////////////////////////////////////////////////////////////////////////////////////////////////////////


theString="0";
theInt=0;
theBool=0;

myPlayer.setUniqueID(theString);
myPlayer.setPlayerName(theString);
myPlayer.setPlayerMinAttributes(theInt,1);

//write to the file
file.write((char*)&amp;myPlayer,sizeof(barePlayer));

file.close();

}

void displayPlayers(){

file.open("Player.txt", ios::in);//open file for reading


for(int y=0;y<2;y++){

file.seekg( y * sizeof(barePlayer),ios::beg);

file.read((char*)&amp;myPlayer,sizeof(barePlayer));

cout << "Player name : "<< myPlayer.getPlayerName() <<endl;
cout << "Player movement : "<< myPlayer.getPlayerMinAttributes(2) <<endl;
cout << "Player strength : "<< myPlayer.getPlayerMinAttributes(3) <<endl;
cout << "Player magic : "<< myPlayer.getPlayerMinAttributes(4) <<endl;

}


file.close();



}it is player.h,player.cpp and main in that order.
Thanks again for any help,

Lemmy.

Dol AI with 3D Bone system
10-13-2004, 04:52 AM
I use English language is poor.

OK Ummmm ...,

Where you behaviour for that character to activate per scene?

But I use Delphi for make the game.
The next time I will exchange idea with you.

Sorry I cannot change Thai to English for information to you.

Carjay
10-13-2004, 07:38 AM
Maybe you need to use ios::binary as well.

But... the connection to OpenGL is exactly ... where?
:)

Lemmy
10-13-2004, 12:13 PM
None ! But lets face it, there are some excellent coders here. Gonna try that now thanks for your help.Will try to make my posts more opengl specific, just trying to get this c++ stuff out of the way first. :)

Lemmy
10-13-2004, 12:22 PM
Hey, tried that and its still not working.the first nane printed is still coming out as *

Any ideas ??
:confused:

Lemmy
10-13-2004, 12:23 PM
Hey, tried that and its still not working.the first name printed is still coming out as *

Any ideas ??
:confused:

Inquisitor
10-15-2004, 11:37 PM
I don't have time to check all your code, but
an obvious mistake is in this line:

file.read((char*)&myPlayer,sizeof(barePlayer));

By that you are overwriting your object's attributes leaving a complete mess in memory, because there are pointers involved (in string).