I have this assignment, and I've hit a wall. Basically its to make uucode, like in Cygwin. I'm working on decoding, and can do that successfully. The problem comes when I try to decode more than one line, my fwrite call simply does not work. Nothing at all prints to stdout. When I tried to debug this using a few printf statements, I noticed fwrite suddenly printed the buffer when the outer loop printf statement was added. I know its not the best methods, but I'm learning. Code below:
int uudecode(const char* InputFilename)
{
FILE* input = fopen(InputFilename, "rb");
if(input)
{
unsigned char buffer[62] = {0}; //buffer to read into
unsigned char output[62] = {0}; //output buffer
unsigned char* pbuf = buffer; //*pointers to each
unsigned char* outbuf = output; //*required for assignment
fgets((char*)buffer, 61, input); //reads unneeded first line
while(!feof(input))
{
printf("OUTERLOOP\n"); //This printf causes the fwrite below to work
fread(buffer, sizeof(unsigned char), 61, input);
// Right and left shift variables, ints to keep track of characters
int rshift = 4, lshift = 2, num_written = 0, linechars = 0;
if(*pbuf == 96) // If a lone backquote is found, we are at the end
break;
//First character notes how many are in a line
linechars = (*pbuf) - 32;
pbuf++;
while(linechars--)
{
...Decode Line...
}
//This is the offending fwrite. Works if the marked printf is present
fwrite(output, sizeof(unsigned char), num_written, stdout);
}
}
else
{
printf("Error opening file");
return -1;
}
return 0;
}
If it will help, here is the test text I'm working with:
//Encoded text:
begin 644 input-remote-1.txt
M86)C9&5F9VAI:FML;6YO<'%R<W1U=G=X>7HP,3(S-#4V-S@Y86)C9&5F9VAI
2:FML;6YO<'%R<W1U=G=X>7H*
`
end
//Decoded text:
abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz
I have tried shorter text to decode, and it seems to work fine. When the fwrite does appear and the output is printed, the decoded text is correct. I can provide that inner loop code if it might be causing the problems. I just have no idea where to go from here.
So directly, my questions are 1) What might be causing this, and 2) How can I fix it?
Thanks in advance!