Skip to main content
gamedev.net
🔒 Locked

Function causes crash...

Started by zackriggle 1 reply 350+ views
Original Post
zackriggle
zackriggle
This specific function (I test each one individually) causes my program to crash... why? I beleive it has something to do with pointers.
  
class MQ_FILE
{
public:
	long start;
	long end;
	long size() { return end - start; };
	char name[20]; // name of the file -- xxxxx.mqo -- used to identify files, as well as strip them apart when

					// stripping the archive...

};

class MQ_ARCHIVE
{
public:
	char name[20];
	MQ_HEADER header;
	std::vector <MQ_FILE> files;
};

void DATA_API mqCreateArchive(MQ_ARCHIVE mqarc)
{
	//////////////WRITE FIRST PART OF HEADER///////////////

	//

	ofstream fout;
	fout.open(mqarc.name);
	mqarc.header.type = MQHEAD_FILETYPE_ARCHIVE;
	mqarc.header.version = 1;
	mqarc.header.files = mqarc.files.size();
	mqarc.header.endheader = 2048;

	fout << int2hex(mqarc.header.type) << endl;
	fout << int2hex(mqarc.header.version) << endl;
	fout << int2hex(mqarc.header.files) << endl;
	fout << int2hex(mqarc.header.endheader) << endl;
	fout << mqarc.name << endl;
	//

	//////////////////////////////////////////////////////




	/////////CALC START AND END POINTS OF EACH FILE IN ARCHIVE/////////

	// Calculate start/end points

	long start = 0;
	long end = 0;
	long last = 2048; // Get current position

	int i = 0;
	ifstream fbuf;
	char* buf;
	buf = new char [6];
	for(i = 0; i != mqarc.files.size(); i++)
	{
		// Get file size

		fbuf.open(mqarc.files[i].name);
		fbuf.seekg(0,ios::end); // Get position of last char (total length)


		// Enter vars....

		mqarc.files[i].start = last + 1;
		mqarc.files[i].end = mqarc.files[i].start + fbuf.tellg();
		last = mqarc.files[i].end;

		fbuf.close();

		// Write vars....

		fout << mqarc.files[i].name << endl;
			// Convert the decimal-length to a hex-length in ASCII and write

			strcpy(buf,int2hex(mqarc.files[i].start));
			fout << buf << endl;
			strcpy(buf,int2hex(mqarc.files[i].end));
			fout << buf << endl;
	};
	// delete[] buf;

	//

	////////////////////////////////////////////////////////////////////



	///////////MAKE HEADER 2048 BYTES LONG////////////

	// The end of the header MUST be at 2047...

	//

	if(fout.tellp() <= (2046 - strlen("### MUST MAKE HEADER == 2047 BYTES ###")))
	{
		fout << "###_MUST_MAKE_HEADER_==_2047_BYTES###";
	}
	while(fout.tellp() != 2046)
	{
		fout << ''#'';
	}
	fout << endl; // The endl is char 2047...

	// now we write the data, starting at 2048

	//////////////////////////////////////////////////


	/////////WRITE DATA///////////////////////////////

	//

	buf = new char [2048]; // 2k buffer

	for(i = 0; i != mqarc.files.size(); i++)
	{
		fbuf.open(mqarc.files[i].name);
		while(!fbuf.eof())
		{

			fbuf.getline(buf,2048);
			fout << buf << endl;

		};
		fbuf.close();
	};
	delete [] buf;	
	//

	//////////////////////////////////////////////////

};
  
================== My (soon-to-be) Cherished Cookie of Appreciation: -- MattB - for WinSock advice --
civguy
civguy

    
buf = new char [6];

should be

       
buf = new char [7];

(remember, c style strings end with '\0')

[edited by - civguy on December 26, 2002 3:15:03 PM]
civguy
civguy
dont forget to uncomment
// delete[] buf;

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.