This is a Windows program, but the question I have is not Windows specific.
I have here a file encoder. You pass the file you want encoded into the file param and you're all set... in a perfect world. The problem is, I cannot get the program to stop writing when the file is done. It writes for the whole buffer size. this is the only way I could get it to work because I don't know how to get file size. Any ideas?
Oh yeah, it writes a byte to tell the decoder how to decode, just to note for you.

Code:
#define MyBufferLength 1024000
#define WIN32_LEAN_AND_MEAN
//
//INCLUDES//
//
int WINAPI WinMain(HINSTANCE hInstance,
				   HINSTANCE hPrevInstance,
				   LPSTR lpCmdLine,
				   int nCmdShow)
{
	srand(time(NULL));//Randomize seed (based on computer clock time)
	char mod = rand() % (3 - 25 + 2);//Random Modifier (key to the encoding)

    char* FileBuffer = new char[MyBufferLength];//File read into this buffer
	char* EncodedFile = new char[50];//Name for new encoded file
	char ModChar[1];
	FILE *fd = fopen(lpCmdLine,"r+");
	
	ModChar[0] = mod;
				
	if( fd == NULL ) 
	{
		MessageBox(HWND_DESKTOP,"Error opening file","Bah!",NULL);
		return 1;
	}
	fread(FileBuffer,sizeof(char),MyBufferLength,fd);
		
	for(int x = 0; x < MyBufferLength; x++)
	{
		FileBuffer[x] = FileBuffer[x] + mod;
	}

	rewind(fd);
	fwrite(ModChar,sizeof(char),sizeof(char),fd);
	fwrite(FileBuffer,sizeof(char),MyBufferLength/*needs to be length of file*/,fd);
	//END
	delete []FileBuffer;
	return 0;
}