I was going through old programming tests from school and cam across this one that I got wrong and was wondering if anybody could help me out.

We maintain our user list as an encrypted text file with the extension .LBE. We have a customer that is reporting that their lbe file it the correct size but, is somehow being very rarely and periodically written with all 0's. please modify the following read and write algorithms to solve or work around this intermittent issue. (hint: Memory is not at a premium) please do the work in a compiler and submit the .h and .cpp files with your test. (do not concern yourself with the "Cypher" or “XylocMemcopy” functions, you can assume that they work properly 100% of the time.)

long SystemFile::LoadList(SystemNode* mn)
{

	SystemNode*		node;
	char systemVersion[16];
   	FILE*			fs;
	// Clear the current list (if any)
	DeleteAll();

	// Open the default file
	// Note::assumes that any existing file is encrypted
	fs = fopen(pathFile,"rb");

	// The data is encryted records.  Thus, the format of the file
	//  to be as follows:
	//
	// < Record 1>
	// < Record 2>
	// < Record 3>
	//  . . . . .
	// < Record n>
	if ( fs != NULL )
	{
		fread((void*)systemVersion,15,1,fs);
		systemVersion[15] = 0;
		if(_stricmp(systemVersion,"Version 2.0") != 0)
		  return(-1);

		// Begin reading in the records and unencrypting them
		node = (SystemNode*)malloc(sizeof(SystemNode));
   		while ( fread((void*)node,8192,1,fs) )
		{
			// Unencrypt the record
   			Cipher((unsigned char*)node,8192,FALSE);

			// Add the record to the linked list
   			SystemList::Add(node);
			node = (SystemNode*)malloc(sizeof(SystemNode));
		}

		free (node);
		fclose(fs);
	}
	return(0);
}
//---------------------------------------------------------------------------
void SystemFile::StoreList(SystemNode* mn)
{
	long			i, num = SystemList::Items();
	SystemNode 	node, *tmp  = NULL;
   	FILE*			fs;

	if ( ! num )
   		return;

	char systemVersion[15] = {'V','e','r','s','i',
	  'o','n',' ','2','.','0','\0','\0','\0','\0'};

	// Save the data out to disk
	fs = fopen(pathFile,"wb");

	
	// Print the number of records and then all records
	if ( fs != NULL )
	{
    fwrite((void*)systemVersion,15,1,fs);
		// Encrypt each record and save it to the file
		for ( i = 0; i < num; i++ )
		{
   			// Get the next record
     		tmp = (SystemNode*)SystemList::GetNode(i);
			xyloc_memcpy((unsigned char*)&node,(unsigned char*)tmp,8192);

	     	// Encrypt the record
	  		Cipher((unsigned char*)&node,8192,TRUE);

			// Write the record to disk
			fwrite((void*)&node,8192,1,fs);
	  	}

		fclose(fs);
	}
	}

I wish I had that structure I would assume it would have to be that size although would just using the sizeof(SystemNode) be more effective. Would that cause the occasional zeroing out of data? That is all the code we got to look at.

Edited 4 Years Ago by kspecks: n/a

Hard to tell without seeing the structure. Also you should be checking the return value of SystemList::GetNode() for NULL so that the program doesn't try to write out a NULL pointer.

Yeah that is all the code we get to look at I am terrible at looking at other peoples code and interpreting it. Much easier to have working code so it can be debugged 1000 times and see where it is failing even if it is once. Thanks for the help guys.

This question has already been answered. Start a new discussion instead.