0

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!

VMess
  • 3
  • 3
  • 2
    Writes to `stdout` are line buffered. You will see the output if you print a `'\n'` to `stdout`. – R Sahu Jan 21 '17 at 05:01
  • I tried adding an arbitrary printf with a newline just above fwrite, and it still printed nothing out. Is that what you meant? – VMess Jan 21 '17 at 05:29
  • 2
    No, it has to be after the call to `fwrite`. – R Sahu Jan 21 '17 at 05:35
  • 2
    In the interest of avoiding a potentially unwanted newline in your stdout dump, [**`fflush`**](http://en.cppreference.com/w/c/io/fflush) ftw. – WhozCraig Jan 21 '17 at 06:35
  • Please [read this why your while clause is wrong](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – Ed Heal Jan 21 '17 at 18:06

2 Answers2

3

the I/O streams stdout, stdin, stderr are all buffered.

So nothing will be output until either: 1) the output buffer overflows 2) a newline sequence `'\n' is output 3) fflush is called for the specific output stream. 4) a input statement is executed or 5) the program exits.

If you want to avoid extra newlines being output, then suggest using:

fflush( stdout );
user3629249
  • 16,402
  • 1
  • 16
  • 17
1

Your while loop condition is incorrect:

Community
  • 1
  • 1
chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • Thanks for the tip! It didn't solve this specific issue, but did help elsewhere. I'll remember this for the future. – VMess Jan 21 '17 at 20:39