Lab Assignment -- C++ File IO and Arg Lists


Objective:


Your main should look something like this

int main()
{
	fileObj obj1;
	fileObj obj2;
	fileObj obj3;
	fileObj copyObj1;
	fileObj copyObj2;
	fileObj copyObj3;

	InitFileObj(obj1);
	InitFileObj(obj2);
	InitFileObj(obj3);

	CopyFileObj(copyObj1, obj1);
	CopyFileObj(copyObj2, obj2);
	CopyFileObj(copyObj3, obj3);

	PrintFileObj(obj1);
	PrintFileObj(obj2);
	PrintFileObj(obj3);

	if(CompFileObjs(obj1, copyObj1))
	{
		std::cout << "Files the same!" << std::endl;
	}
	else
	{
		std::cout << "Files different!" << std::endl;
	}

	WriteFileObj(obj1);
	WriteFileObj(obj2);
	WriteFileObj(obj3);

	InitFileObj(obj1);
	InitFileObj(obj2);
	InitFileObj(obj3);

	if(CompareFileObjs(obj1, copyObj1))
	{
		std::cout << "Files the same!" << std::endl;
	}
	else
	{
		std::cout << "Files different!" << std::endl;
	}

	ReadFileObj(obj2);
	ReadFileObj(obj3);
	ReadfileObj(obj1);

	if(CompareFileObjs(obj1, copyObj1))
	{
		std::cout << "Files the same!" << std::endl;
	}
	else
	{
		std::cout << "Files different!" << std::endl;
	}
	
	return 0;
}

It may be necessary to modify the parameters of some of the functions above.

Do whatever you feel is necessary to get the functions to work properly.

If you have questions on exactly what the functions should do ask during the lab.

Have more verbose output messages that explain what is going on and if the program is working properly.

Try to break your program by using it in ways other than exactly how it was intended to be used.

You must be able to prove that the code you have written is working as requested to get marks.


Deliverables:

Same Solution: This is not the only solution, this is just the solution I came up with when figuring out how to do binary file i/o.

There are numerous optimizations that can be made. Also the main() has some other stuff in there because I was experimenting with it.

#include 
#include 

#define WIN32_LEAN_AND_MEAN
#ifdef WIN32_LEAN_AND_MEAN
	#include 
#endif

#include 

#include 

const int NEW_FILE = -1;
const int MAX_CHAR_DATA_SIZE = 256;

struct fileObj
{
	int sizeCharData;
	char charData[MAX_CHAR_DATA_SIZE];
	double double1;
	double double2;
	double double3;
};

struct indexBlock
{
	int startByte;
	int numBytes;
};

void InitFile(fileObj& theObj, char data);
int ReadFile(int fileId, fileObj& theObj);
int WriteFile(int fileId, fileObj& theObj);
void PrintFile(fileObj& theObj);

// Generates a random int between minInt and minInt+intRange-1
int GenerateRandomInt(int minInt, int intRange);
// Generates a random double between minDouble and 
minDouble+doubleRange-1.0
double GenerateRandomDouble(double minDouble, double doubleRange);

int GetSizeFileObj(fileObj& theObj);

double rMax = static_cast(RAND_MAX+1);

std::list indexBlockList;

int main()
{
#ifdef WIN32_LEAN_AND_MEAN
	srand(GetTickCount());
#else
	srand(123456789);
#endif

	//// Create some fileObj 
	//fileObj obj1;
	//fileObj obj2;
	//fileObj obj3;
	//fileObj obj4;

	//// initialize fileobj
	//initfile(obj1, 'r');
	//initfile(obj2, 's');
	//initfile(obj3, 'v');
	//initfile(obj4, 'p');

	//// Calculate size of fileObj
	//int sizeObj1 = GetSizeFileObj(obj1);
	//int sizeObj2 = GetSizeFileObj(obj2);
	//int sizeObj3 = GetSizeFileObj(obj3);
	//int sizeObj4 = GetSizeFileObj(obj4);

	//// Print fileObj
	//std::cout << "Obj1: " << std::endl;
	//PrintFile(obj1);
	//std::cout << "Obj2: " << std::endl;
	//PrintFile(obj2);
	//std::cout << "Obj3: " << std::endl;
	//PrintFile(obj3);
	//std::cout << "Obj4: " << std::endl;
	//PrintFile(obj4);

	//// Save fileObj
	//int hndl1 = WriteFile(obj1); //*/ 0;
	//int hndl2 = WriteFile(obj2); //*/ 131;
	//int hndl3 = WriteFile(obj3); //*/ 305;
	//int hndl4 = WriteFile(obj4); //*/ 400;

	//// Re-Initialize fileObj
	//InitFile(obj1, 'a');
	//InitFile(obj2, 'b');
	//InitFile(obj3, 'c');
	//InitFile(obj4, 'd');

	//int newSizeObj1 = GetSizeFileObj(obj1);
	//int newSizeObj2 = GetSizeFileObj(obj2);
	//int newSizeObj3 = GetSizeFileObj(obj3);
	//int newSizeObj4 = GetSizeFileObj(obj4);

	//// Print fileObj, now all different
	//std::cout << "Modified Obj1: " << std::endl;
	//PrintFile(obj1);
	//std::cout << "Modified Obj2: " << std::endl;
	//PrintFile(obj2);
	//std::cout << "Modified Obj3: " << std::endl;
	//PrintFile(obj3);
	//std::cout << "Modified Obj4: " << std::endl;
	//PrintFile(obj4);

	//std::cout << "**********" << std::endl;

	//if(sizeObj1 == newSizeObj1)
	//{
	//	std::cout << "OBJ1 SAME SIZE" << std::endl;
	//}
	//else
	//{
	//	std::cout << "OBJ1 DIFFERENT SIZE" << std::endl;
	//}

	//if(sizeObj2 == newSizeObj2)
	//{
	//	std::cout << "OBJ2 SAME SIZE" << std::endl;
	//}
	//else
	//{
	//	std::cout << "OBJ2 DIFFERENT SIZE" << std::endl;
	//}

	//if(sizeObj3 == newSizeObj3)
	//{
	//	std::cout << "OBJ3 SAME SIZE" << std::endl;
	//}
	//else
	//{
	//	std::cout << "OBJ3 DIFFERENT SIZE" << std::endl;
	//}

	//if(sizeObj4 == newSizeObj4)
	//{
	//	std::cout << "OBJ4 SAME SIZE" << std::endl;
	//}
	//else
	//{
	//	std::cout << "OBJ4 DIFFERENT SIZE" << std::endl;
	//}

	//std::cout << "**********" << std::endl << std::endl;

	//// Read fileObj in different order than was written
	//newSizeObj1 = ReadFile(hndl1, obj1);
	//newSizeObj2 = ReadFile(hndl2, obj2);
	//newSizeObj3 = ReadFile(hndl3, obj3);
	//newSizeObj4 = ReadFile(hndl4, obj4);

	//// Print fileObj, now same as first print
	//std::cout << "Obj1 After Read: " << std::endl;
	//PrintFile(obj1);
	//std::cout << "Obj2 After Read: " << std::endl;
	//PrintFile(obj2);
	//std::cout << "Obj3 After Read: " << std::endl;
	//PrintFile(obj3);
	//std::cout << "Obj4 After Read: " << std::endl;
	//PrintFile(obj4);

	//std::cout << "**********" << std::endl;

	//if(sizeObj1 == newSizeObj1)
	//{
	//	std::cout << "OBJ1 SAME SIZE" << std::endl;
	//}
	//else
	//{
	//	std::cout << "OBJ1 DIFFERENT SIZE" << std::endl;
	//}

	//if(sizeObj2 == newSizeObj2)
	//{
	//	std::cout << "OBJ2 SAME SIZE" << std::endl;
	//}
	//else
	//{
	//	std::cout << "OBJ2 DIFFERENT SIZE" << std::endl;
	//}

	//if(sizeObj3 == newSizeObj3)
	//{
	//	std::cout << "OBJ3 SAME SIZE" << std::endl;
	//}
	//else
	//{
	//	std::cout << "OBJ3 DIFFERENT SIZE" << std::endl;
	//}

	//if(sizeObj4 == newSizeObj4)
	//{
	//	std::cout << "OBJ4 SAME SIZE" << std::endl;
	//}
	//else
	//{
	//	std::cout << "OBJ4 DIFFERENT SIZE" << std::endl;
	//}

	//std::cout << "**********" << std::endl << std::endl;

	fileObj obj1;
	fileObj obj2;

	InitFile(obj1, 'a');
	InitFile(obj2, 'b');

	std::cout << "STEP 1" << std::endl;
	PrintFile(obj1);

	int hndl1 = WriteFile(NEW_FILE, obj1);
	int hndl2 = WriteFile(NEW_FILE, obj2);

	InitFile(obj1, 'c');
	InitFile(obj2, 'd');

	std::cout << "STEP 2" << std::endl;
	PrintFile(obj1);

	ReadFile(hndl1, obj1);

	std::cout << "STEP 3" << std::endl;
	PrintFile(obj1);

	obj1.double1 = -99.9;
	obj1.double2 = -99.9;
	obj1.double3 = -99.9;

	int hndl3 = WriteFile(hndl1, obj1);

	if(hndl1 != hndl3)
	{
		int xxx = 0;
		++xxx;
	}

	ReadFile(hndl3, obj1);
	ReadFile(hndl1, obj2);

	std::cout << "STEP 4" << std::endl;
	PrintFile(obj1);
	PrintFile(obj2);

	return 0;
};

void InitFile(fileObj& theObj, char data)
{
	for(int i = 0 ; i < MAX_CHAR_DATA_SIZE ; ++i)
	{
		theObj.charData[i] = '0';
	}
	theObj.sizeCharData = GenerateRandomInt(0, MAX_CHAR_DATA_SIZE);
	for(int j = 0 ; j < theObj.sizeCharData ; ++j)
	{
		theObj.charData[j] = data;
	}
	theObj.double1 = GenerateRandomDouble(0.0, 100.0);
	theObj.double2 = GenerateRandomDouble(100.0, 100.0);
	theObj.double3 = GenerateRandomDouble(200.0, 100.0);
}

int ReadFile(int fileId, fileObj& theObj)
{
	std::fstream inFile;
	inFile.open("hardDisk.txt", std::ios_base::in | 
std::ios_base::binary);
	if(!inFile.is_open())
	{
		return -1;
	}

	inFile.seekg(fileId, std::ios_base::beg);

	int amountRead = 0;
	char readBuffer[256];

	inFile.read(readBuffer, sizeof(double));
	amountRead+=inFile.gcount();
	theObj.double1 = *(reinterpret_cast(readBuffer));

	inFile.read(readBuffer, sizeof(double));
	amountRead+=inFile.gcount();
	theObj.double2 = *(reinterpret_cast(readBuffer));

	inFile.read(readBuffer, sizeof(double));
	amountRead+=inFile.gcount();
	theObj.double3 = *(reinterpret_cast(readBuffer));

	inFile.read(readBuffer, sizeof(int));
	amountRead+=inFile.gcount();
	theObj.sizeCharData = *(reinterpret_cast(readBuffer));

	inFile.read(readBuffer, theObj.sizeCharData);
	amountRead+=inFile.gcount();
	strncpy_s(theObj.charData, MAX_CHAR_DATA_SIZE, readBuffer, 
theObj.sizeCharData);

	inFile.close();
	return amountRead;
}

int WriteFile(int fileId, fileObj& theObj)
{
	std::fstream outFile;
	outFile.open("hardDisk.txt", std::ios_base::in | 
std::ios_base::out | std::ios_base::binary);
	if(!outFile.is_open())
	{
		return -1;
	}

	indexBlock newIndexBlock;
	newIndexBlock.numBytes = 0;
	newIndexBlock.startByte = fileId;

	if(fileId == NEW_FILE)
	{
		newIndexBlock.startByte = 0;
		std::list::iterator iter;
		for(iter = indexBlockList.begin() ; iter != 
indexBlockList.end() ; ++iter)
		{
			newIndexBlock.startByte+=(*iter).numBytes;
		}
	}

	int startPos = 0;
	int endPos = 0;

	outFile.seekp(newIndexBlock.startByte, std::ios_base::beg);

	startPos = outFile.tellp();

	outFile.write(reinterpret_cast(&theObj.double1), 
sizeof(double));
	outFile.write(reinterpret_cast(&theObj.double2), 
sizeof(double));
	outFile.write(reinterpret_cast(&theObj.double3), 
sizeof(double));
	outFile.write(reinterpret_cast(&theObj.sizeCharData), 
sizeof(int));
	outFile.write(theObj.charData, theObj.sizeCharData);

	endPos = outFile.tellp();

	newIndexBlock.numBytes = endPos - startPos;

	if(fileId == NEW_FILE)
	{
		indexBlockList.push_back(newIndexBlock);
	}

	outFile.close();
	return newIndexBlock.startByte;
}

void PrintFile(fileObj& theObj)
{
	std::cout << "CharData: ";
	for(int i = 0 ; i < theObj.sizeCharData ; ++i)
	{
		std::cout << theObj.charData[i];
	}
	std::cout << std::endl;
	std::cout << "Double1: " << theObj.double1 << std::endl;
	std::cout << "Double2: " << theObj.double2 << std::endl;
	std::cout << "Double3: " << theObj.double3 << std::endl;
	std::cout << "SizeCharData: " << theObj.sizeCharData << std::endl 
<< std::endl;
}

// Generates a random int between minInt and minInt+intRange-1
int GenerateRandomInt(int minInt, int intRange)
{
	return minInt + 
static_cast((static_cast(rand())/rMax)*static_cast(intRange));
}

// Generates a random double between minDouble and 
minDouble+doubleRange-1.0
double GenerateRandomDouble(double minDouble, double doubleRange)
{
	return minDouble + (static_cast(rand())/rMax)*doubleRange;
}

int GetSizeFileObj(fileObj& theObj)
{
	int sum = 0;
	sum+=sizeof(theObj.double1);
	sum+=sizeof(theObj.double2);
	sum+=sizeof(theObj.double3);
	sum+=sizeof(theObj.sizeCharData);
	sum+=theObj.sizeCharData;
	return sum;
}