Menu

writing ONE byte x00A to file????

2002-12-15
2012-09-26
  • Nobody/Anonymous

    Hi,

    I'm currently writing an image exporter in which i have to end the header with a "\n" before the raw data is written. The problem is that on win32 this equals to x00D x00A (that's hex value D followed by A). I need unix style, with only the hex A. I replaced the "\n" by one byte with value and still got the 2 bytes?????

    here is some code:
    #include <fstream>
    using namespace std;
    int main(int argc, char *argv[]) {
      ofstream ofile("testing.txt");   // open testing file
      char * onebyte;                  // unsigned so we can enter a number
      onebyte=new char;                // allocate ONE byte
      onebyte[0]=10;                   // 10=x00A (unix style \n)
      ofile.write(onebyte,1);          // write ONE byte
      ofile.close();                   // close it
      return 0;                        // the end
    }

    If you compile with gcc 2.95 or 3.2 and run it, a file with size 2 will be generated. Using HexEdit you can see that it's the D and then A. If i replace in the code the 10 by 11, then i get a file size one, only containing hex value B (which is 11 decimal).

    How can i write one byte containing only x00A with the fstream????

    stephan

     
    • Nobody/Anonymous

      nevermind, i found it.....
      ofstream ofile("testing.txt",ofstream::binary);
      does the trick. The meansing of binary here is the same as in ftp, that's what i learned after more than one hour tweaking :-(

      stephan

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.