Jump to content
  • Advertisement
Sign in to follow this  
Alpha Nox

Read/Writing a vector into a file

This topic is 4655 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I'm trying to make a character creator program,which store a vector into a file to be read later by the GAME SERVER. I can only write for the first time, when I try to write it for the second time it gives an error when it tries to open the chardb.dat for reading. I would really like help with this. Thanks very much, Here's the code:
#include "stdafx.h"
#include <iostream>
#include <tchar.h>
#include <iostream>
#include <tchar.h>
#include <string>
#include <vector>
#include "NetworkStructures.h" 
#include "SHA1.cpp" 
#include "GetTime.h" 
#include "BitStream.h"
#include "RakNetworkFactory.h"
#include "RakClientInterface.h"
#include "RakServerInterface.h"
#include "RakPeerInterface.h" 
#include "NetworkTypes.h"
#include "Multiplayer.h"
#include "PacketEnumerations.h"
#include <assert.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include "string"
#include "fstream"

class build_char { 
public:
	PlayerID playerId;
	build_char();
	build_char(int setX,int setY,int setpos);
	build_char(char* monstertype);
	int X;
	int Y;
	int frame;
	int pos;
	int movespeed;
	char name[40];
	char* type;
	char* level;
    int charlevel;	
    long spritesurface;

	// CHARACTER STATUS

	bool ISbleeding;
	int bleedcounter;

	bool ISsaying;
	char saying[100];
	int saycounter;

	bool ISfiring;
	int firingcounter;

	bool swordatk;
	int swordcounter;


	// NPC AI
	bool stuck;
	bool NPCinVIDEO;
	
	
	int framecounter;
	void walkdown();
	void walkup();
	void walkleft();
	void walkright();

	
	int health;
	int maxhealth;
	int mana;
	int maxmana;
	int stamina;
	int maxstamina;

	int staminacounter;
	int staminaspeed;
	int staminarecovercounter;
	int staminarecoverspeed;

	bool dead;
	int swordDMG;
	int swordspeed;
	bool gotsword;
	int damage;

	bool alreadydeathFX;
	
	int spritesleepX;
	int spritesleepY;

	int spritesleepW;
	int spritesleepH;


	int sprite2X[5];
	int sprite8X[5];
	int sprite4X[5];
	int sprite6X[5];
	
	int sprite2Y[5];
	int sprite8Y[5];
	int sprite4Y[5];
	int sprite6Y[5];
	int spriteW;
	int spriteH;
	
};

build_char::build_char() {
	
	NPCinVIDEO = false;
	stuck = false;

	X = 350;
	Y = 250;
	frame = 0;
	pos = 2;
    strcpy(name,"UNKNOWN");	 
	type = "player";
	charlevel = 1;

	health = 100;
	maxhealth = 100;

	mana = 10;
	maxmana = 10;

	stamina = 100;
	maxstamina = 100;
	
	staminacounter = 0;
	staminaspeed = 5;
	staminarecovercounter = 0;
	staminarecoverspeed = 30;

	dead = false;
	swordDMG = 5;
	swordspeed = 2;
	gotsword = false;
	
	movespeed = 13;

	alreadydeathFX = false;
		 	 
	 spritesleepX = 195;
	 spritesleepY = 0;

	 spritesleepW = 33;
	 spritesleepH = 30;

	 sprite2X[1] = 0;
	 sprite2X[2] = 47;
	 sprite2X[3] = 94;
	 sprite2X[4] = 144;

	 sprite8X[1] = 0;
	 sprite8X[2] = 44;
	 sprite8X[3] = 93;
	 sprite8X[4] = 142;

	 sprite4X[1] = 2;
	 sprite4X[2] = 52;
	 sprite4X[3] = 100;
	 sprite4X[4] = 148;

	 sprite6X[1] = 0;
	 sprite6X[2] = 42;
	 sprite6X[3] = 92;
	 sprite6X[4] = 142;

	 sprite2Y[1] = 0;
	 sprite2Y[2] = 0;
	 sprite2Y[3] = 0;
	 sprite2Y[4] = 0;

	 sprite8Y[1] = 191;
	 sprite8Y[2] = 191;
	 sprite8Y[3] = 190;
	 sprite8Y[4] = 191;

	 sprite4Y[1] = 63;
	 sprite4Y[2] = 63;
	 sprite4Y[3] = 63;
	 sprite4Y[4] = 63;

	 sprite6Y[1] = 127;
	 sprite6Y[2] = 127;
	 sprite6Y[3] = 127;
	 sprite6Y[4] = 127;
	
	 spriteW = 46;
	 spriteH = 63;
	 
	 
}

build_char::build_char(int setX,int setY,int setpos){
	X = setX;
	Y = setY;
	pos = setpos;

	alreadydeathFX = false;

}

build_char::build_char(char* monstertype){
	
	alreadydeathFX = false;
	
	frame = 0;
	pos = 2;
    
	movespeed = 15;
	
	
	
	if(monstertype == "souldev")
	{
		
		strcpy(name,"souldev");
		type = "monster";
		//spritesurface = monsters;
		health = 10;
		dead = false;
		damage = 10;

		framecounter = 0;

		X = 100;
		Y = 400;

		sprite2X[1] = 52;
		sprite2X[2] = 4;
		sprite2X[3] = 52;
		sprite2X[4] = 100;

		sprite8X[1] = 52;
		sprite8X[2] = 5;
		sprite8X[3] = 52;
		sprite8X[4] = 100;

		sprite4X[1] = 56;
		sprite4X[2] = 5;
		sprite4X[3] = 56;
		sprite4X[4] = 101;

		sprite6X[1] = 55;
		sprite6X[2] = 5;
		sprite6X[3] = 55;
		sprite6X[4] = 96;

		sprite2Y[1] = 129;
		sprite2Y[2] = 129;
		sprite2Y[3] = 129;
		sprite2Y[4] = 129;

		sprite8Y[1] = 0;
		sprite8Y[2] = 1;
		sprite8Y[3] = 0;
		sprite8Y[4] = 0;

		sprite4Y[1] = 194;
		sprite4Y[2] = 193;
		sprite4Y[3] = 194;
		sprite4Y[4] = 194;

		sprite6Y[1] = 65;
		sprite6Y[2] = 65;
		sprite6Y[3] = 65;
		sprite6Y[4] = 65;
	
		spriteW = 42;
		spriteH = 64;

	}
}


void build_char::walkup(){
	
	pos = 8;
	Y -= movespeed;

}

void build_char::walkdown(){
	
	pos = 2;
	Y += movespeed;

}

void build_char::walkleft(){

	pos = 4;
	X -= movespeed;

}

void build_char::walkright(){
	
	pos = 6;
	X += movespeed;

}



using namespace std;



build_char newcharacter;
vector<build_char> playersLOGIN;
vector<build_char>::iterator itr;
string cmd;

void WriteNewChar();

ofstream fout;
ofstream fout2;
ifstream fin;

int main()
{
	cout << "Welcome to PHOENIX Online CHARACTER creator.\n";
	cout << "Please make sure you already created a login for the new character.\n";
	cout << endl;
	
	cout << "Player CHARACTER NAME(same as in login): ";
	cin >> newcharacter.name;

	cout << "Do you want all the others settings to be default?(y/n): ";
	cin >> cmd;

	if(cmd == "y")
	{
		
		
	}
	else
	{

	}

	
	cout << "WARNING: BACKUP GAME FILES AND CLOSE THE SERVER BEFORE PROCEEDING!!! RISK OF DATA LOSS!!!\n";
	cout << "Are you sure you want to create a new char?(y/n): ";
	cin >> cmd;
	if(cmd == "y")
	{
		WriteNewChar();
	}
	else 
	{
		cout << "New character creation cancelled by user.\n";
	}
	

	cout << "\nCharacter creation done.\n";

	system("pause");


	return 0;
}


void WriteNewChar()
{
	long accountnumber = 0;

	bool DOread = true;
	bool DOinforead = true;
	ifstream fin("chardbinfo.dat");
	ifstream chardb("chardb.dat");
	
	if(!chardb.is_open())
	{	
		cout << "CHARACTER DBASE NOT FOUND, CREATING NEW FILE.\n";
		DOread = false;

    }

	if(!fin.is_open())
	{	
		cout << "CHARACTER INFO NOT FOUND, CREATING NEW FILE.\n";
		DOinforead = false;

    }
	
	if(DOinforead == true)
	{
		
		fin >> accountnumber;
		fin.close();
	
	}

	if(DOread == true)
	{
		
		cout << "READING NOW!!!\n";
		chardb.read(reinterpret_cast<char*> (&playersLOGIN), accountnumber * sizeof(build_char));
		chardb.close();
		cout << "READ SUCCESS\n";
	
	}
	
	cout << "opening file chardb.dat\n";
	fout.open("chardb.dat",ios_base::app);
	
	cout << "success\n";
	if(!fout.is_open()) 
	{	
		cerr << "ERROR: COULDN'T OPEN THE LOGIN DBASE FILE.\n";
		system("pause");
	}

	fout2.open("chardbinfo.dat");
	
	if(!fout2.is_open()) 
	{	
		cerr << "ERROR: COULDN'T OPEN THE LOGIN DBASE FILE.\n";
		system("pause");
		exit(1);
	}
	
	cout << "acc num += 1\n";
	accountnumber += 1;
	cout << "success";

	cout << "Creating new character...\n";
	
	
	//build_login newuser(username,password,charname);
	cout << "pushingback\n";
		
	playersLOGIN.push_back(newcharacter);
	
	cout << "success\n";
	cout << "WRITING TO FILE\n";
	
	for(itr = playersLOGIN.begin();itr != playersLOGIN.end();itr++)
	{
		
		fout.write((char*) (&*itr),sizeof(build_char));
	}
	
	//fout.write(reinterpret_cast<char*> (&playersLOGIN),accountnumber * sizeof(build_char));
	cout << "WROTE OK\n";
	cout << "NEW LOGIN CREATED SUCCESSFULLY.\n";
	
	cout << "WRITING ACC NUM\n";
	fout2 << accountnumber; 
	cout << "success\n";


	
}

[Edited by - Fruny on September 24, 2005 4:48:07 PM]

Share this post


Link to post
Share on other sites
Advertisement
Even though the code has a number of minor "issues" and possible design improvements i'll just get straight to the point as to what "i think" could be the answer to your question.

When you close a file stream before reopening you need to clear the iostream state flags it's because re-opening a file stream does not clear the state flags, it's a known technical issue in the C++ standard itself. You can clear/reset all state flags by calling std::basic_ios::clear (file stream types are indirect sub-types of std::basic_ios so they too have the clear method).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!