FileOutputStream write() function Writes Incorrect Data

When using the FileOutputStream write() function, writing data greater than 0x7F results in incorrect data being written to the output file. The output data displayed below should start at 0x00 and increment by one except at position 0x00000020 where I attempted to write a value of 0x80 to clearly show whats being written instead of 0x80 (i.e. e2 82 ac). The data continues correctly starting with a value of 0x21 after that. Incorrect data is then written again when the data to be written exceeds 0x7f at location 0x00000082.

Below is an image of the data as seen in the debugger followed by an image of the file contents read by the Notepad++ hex editor and following that is the code that writes the data.

I’m developing using Win 7 and JUCE 5.4.1 and I use the write() function in other parts of my program with no problem but for some reason it fails here. Since I’m writing data that is not always an ASCII character, do I need to open the file in binary mode?

   UINT     i ;
   UINT8    TestBuffer[400] ;

   // Write record buffer file.

   strKitWrite = strSessionFolder + "RecordBuffer.kit" ;
   File filename1(strKitWrite) ;
   FileOutputStream output (filename1);

   if (output.openedOk())
   {
      output.setPosition (0);
      output.truncate();
      for (i=0; i<200; i++)
      {
         if (i == 32)
         {
            TestBuffer[i] = 128 ;
         }
         else
         {
            TestBuffer[i] = i ;
         }
      }
      output.write((const void*)TestBuffer, 200) ;
      Success = true ;
   }

It appears the problem is with the Notepad++ editor where it’s performing some type of translation on data greater than 127. I don’t know what that is and it does not seem to always do it but it looks like the problem is not with JUCE. This issue can be closed.

1 Like

UTF8 related?

Yes jimc, I do think it’s UTF8 related but I don’t know why 0x80 gets converted to 0xe2 0x82 0xac. I would expect it to get converted to 0xc2 0x80. I also haven’t found a way to get Notepad++ to skip the conversion so I’m assuming it’s a Notepad++ problem and I’m now using MS Visual Studio to view binary files.