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, '
');
//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*)&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*)&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.