• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Dissipate

Why is txt/bin file being serialised mysterious numbers...?

2 posts in this topic

I am have created an ISerialization inteface that overshadows CBinaryFile and CTextFile that inherit it. I am using boost serialization to save some sample text to a 'text/binary' file. I am overriding and overloading the operator<< for the concrete classes. I think due to this I am getting strange numbers prefixing each output in the text file. I don't know whether the same is happening in the bin file but I assume it is.

 

The headers are left out. This code works fine when run in a test project without any classes - just the basic i/o code. See end of post for output:

class ISerialization
{
public:
	ISerialization(void) {}
	~ISerialization(void) {}

        ifstream m_ifs;
        ofstream m_ofs;

	unsigned int m_iBoostFlags;
	unsigned int m_iIOSFlags;

	boost::shared_ptr<boost::archive::binary_oarchive> m_spBinOA; // Bin Out
	boost::shared_ptr<boost::archive::binary_iarchive> m_spBinIA; // Bin In
	boost::shared_ptr<boost::archive::text_oarchive> m_spTxtOA; // Txt Out
	boost::shared_ptr<boost::archive::text_iarchive> m_spTxtIA; // Txt In
	boost::shared_ptr<boost::archive::xml_oarchive> m_spXmlOA; // Xml Out
	boost::shared_ptr<boost::archive::xml_iarchive> m_spXmlIA; // Xml In
		
	virtual void OpenOutput(std::string& s, unsigned int m_iIOSFlags) = 0;
	virtual void OpenInput(std::string& s, unsigned int m_iIOSFlags) = 0;

	virtual ISerialization& operator<< (const string& s) = 0;
	virtual ISerialization& operator<< (const int i) = 0;
	virtual ISerialization& operator<< (const unsigned int ui) = 0;
	virtual ISerialization& operator<< (const float f) = 0;
	virtual ISerialization& operator<< (const double d) = 0;

	friend ISerialization& operator<< (ISerialization& s, std::ostream& (*manip)(std::ostream &)) 
	{
		if (s.m_ofs.is_open()) 
		{
			manip(s.m_ofs);
			s.m_ofs.flush();
			return s;
		}
		else
			return s;
	}

private:

};

Here is the CBinaryFile class

class CBinaryFile : public ISerialization
{
public:
	CBinaryFile(void) {}
	~CBinaryFile(void) {}

	void OpenOutput(std::string& s, unsigned int m_iIOSFlags) override 
	{
		// Open filestream
		ISerialization::m_ofs = std::ofstream(s.c_str(), m_iIOSFlags | std::ios::binary);

		// Set flags for boost
		ISerialization::m_iBoostFlags = boost::archive::no_header;

		// Create boost binary output archive
		ISerialization::m_spBinOA = 
			boost::make_shared<boost::archive::binary_oarchive>(m_ofs, m_iBoostFlags);
	}

	void OpenInput(std::string& s, unsigned int m_iIOSFlags) override
	{
		// Open filestream
		ISerialization::m_ifs = std::ifstream(s.c_str(), m_iIOSFlags | std::ios::binary);

		// Set flags for boost
		ISerialization::m_iBoostFlags = boost::archive::no_header;

		// Create boost binary output archive
		ISerialization::m_spBinIA = 
			boost::make_shared<boost::archive::binary_iarchive>(m_ifs, m_iBoostFlags);
	}

	ISerialization& operator<< (const string& s) override {
		if (m_ofs.is_open())
			*m_spBinOA << s;
		
		return *(static_cast<ISerialization*>(this));
	}

	ISerialization& operator<< (const int i) override {
		if (m_ofs.is_open())
			*m_spBinOA << i;

		return *(static_cast<ISerialization*>(this));
	}

	ISerialization& operator<< (const unsigned int ui) override {
		if (m_ofs.is_open())
			*m_spBinOA << ui;

		return *(static_cast<ISerialization*>(this));
	}

	ISerialization& operator<< (const float f) override {
		if (m_ofs.is_open())
			*m_spBinOA << f;

		return *(static_cast<ISerialization*>(this));
	}

	ISerialization& operator<< (const double d) override {
		if (m_ofs.is_open())
			*m_spBinOA << d;
		return *(static_cast<ISerialization*>(this));
	}

private:

};

Here is the CTextFile class:

class CTextFile : public ISerialization
{
public:
	CTextFile(void) {}
	~CTextFile(void) {}

	void OpenOutput(std::string& s, unsigned int m_iIOSFlags) override 
	{
		// Open filestream
		ISerialization::m_ofs = std::ofstream(s.c_str(), m_iIOSFlags);

		// Set flags for boost
		ISerialization::m_iBoostFlags = boost::archive::no_header;

		// Create boost binary output archive
		ISerialization::m_spTxtOA = 
			boost::make_shared<boost::archive::text_oarchive>(m_ofs, m_iBoostFlags);
	}

	// NOT IMPLEMENTED as cant read an arbitrary text file.
	//void OpenInput(std::string& s, unsigned int m_iIOSFlags) override
	void OpenInput(std::string& s, unsigned int m_iIOSFlags) override {}
	//{
	//	// Open filestream
	//	ISerialization::m_ifs = std::ifstream(s.c_str(), m_iIOSFlags);

	//	// Set flags for boost
	//	ISerialization::m_iBoostFlags = boost::archive::no_header;

	//	// Create boost binary output archive
	//	ISerialization::m_spTxtIA = 
	//		boost::make_shared<boost::archive::text_iarchive>(m_ifs, m_iBoostFlags);
	//}

	ISerialization& operator<< (const string& s) override {
		stringstream ss;
		ss << s.c_str();
		if (m_ofs.is_open())
			*m_spTxtOA << ss.str();
		
		return *this;
	}

	ISerialization& operator<< (const int i) override {
		if (m_ofs.is_open())
			*m_spTxtOA << i;

		return *(static_cast<ISerialization*>(this));
	}

	ISerialization& operator<< (const unsigned int ui) override {
		if (m_ofs.is_open())
			*m_spTxtOA << ui;

		return *(static_cast<ISerialization*>(this));
	}

	ISerialization& operator<< (const float f) override {
		if (m_ofs.is_open())
			*m_spTxtOA << f;

		return *(static_cast<ISerialization*>(this));
	}

	ISerialization& operator<< (const double d) override {
		if (m_ofs.is_open())
			*m_spTxtOA << d;

		return *(static_cast<ISerialization*>(this));
	}

private:

};

This is how I call them:

int main()
{
	CBinaryFile MyBinaryFile;
	CTextFile   MyTextFile;

	string sBinFile = "MyBinFile.bin";
	string sTxtFile = "MyTxtFile.txt";

	// Open files
	MyBinaryFile.OpenOutput(sBinFile, ios::trunc | ios::binary);
	MyTextFile.OpenOutput(sTxtFile, ios::trunc);

	// Write stuff
	MyBinaryFile << "Hello " << "Should not be " << std::endl <<
		            "Numbers " << "Here" << std::endl;
	MyTextFile << "Bye the way " << "There " << std::endl <<
		          "should be " << " no numbers here either " << std::endl;

	// Close
	MyBinaryFile.CloseOutputFile();
	MyTextFile.CloseOutputFile();

	return 0;
}

Sorry for long post but maybe this will help a few people aswell. I really look forward to you reflective thoughts. Thank you in advance.

This is the Output:

12 Bye the way  6 There 
 10 should be  24  no numbers here either 

0

Share this post


Link to post
Share on other sites

It would look as if the serializing function is outputting the length of the string to be read in when it is deserialized; if they went with null-terminated output, you couldn't store a null character in the string and have it recovered, so they had to include the length somehow.

0

Share this post


Link to post
Share on other sites

Ok, I've rewritten all the overloaded the operator<< to save the each data type to the ostream instead, which got rid of the prefixed spaces in front of any number I may wish to save. 2 quick questions:

1: How could I template overload these operators. I tried but my compiler keeps breaking; and

2: Should I really be doing:

ISerialization& operator<< (const string& s) override {
       /*do Stuff*/	
	return *(static_cast<ISerialization*>(this));
}

Instead of just:

return *this;
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0