Thread: Encoder Issue

  1. #1
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937

    Encoder Issue

    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;
    }
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  2. #2
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    You could write until feof() or read the position of the file with:

    off_t fileLength = lseek(fd, 0L, SEEK_END);

  3. #3
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Ah, thank you. What needs to be included to use that line of code?
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  4. #4
    #include <stdio.h>

  5. #5
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    not working... doesn't recognize identifiers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. float calculation issue
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-26-2008, 04:56 AM
  2. type safe issue
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 02-12-2008, 09:32 PM
  3. directsound issue
    By valis in forum Tech Board
    Replies: 0
    Last Post: 06-25-2006, 09:28 PM
  4. Zebra P310 Encoder
    By newgirl in forum Networking/Device Communication
    Replies: 3
    Last Post: 10-25-2004, 12:38 AM
  5. my first issue of GDM
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 09-12-2002, 04:02 PM