Specifying binary output?

This is a discussion on Specifying binary output? within the C++ Programming forums, part of the General Programming Boards category; Hi, Ive made a program which should output som binary data. The problem is that more bytes than I specify ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    29

    Specifying binary output?

    Hi,

    Ive made a program which should output som binary data. The problem is that more bytes than I specify gets out, and somehow the data seems a little corrupt. Its only a problem under Windows, so Ill guess its because there is some translation of bytes in the run. The code is:
    Code:
    bool ppmImage::WriteRaw(ostream& out){
    	int size;
    	if((width==0)&&(height==0)){return false;}
    	size=width;
    	size*=height;
    	size*=3;
    	out.write((const char*) data_p,size);
    	return true;
    }
    Is there a way I can specify, that output should be considered binary, like:
    Code:
    out.settype(BINARY);
    or an equivalent function??

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,646
    Use "ios::binary" in the mode when you open the file. It will be ignored on other OS's that don't do newline translations.

    gg

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    29
    And what if it is standard output? How do I specify "cout" to be binary?

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,646
    You will have to use setmode() on stdout. This is Windows specific so....

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting 32 bit binary IP to decimal IP (vice-versa)
    By Mankthetank19 in forum C Programming
    Replies: 15
    Last Post: 12-28-2009, 06:17 PM
  2. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 02:08 PM
  3. why this output?
    By dredre in forum C Programming
    Replies: 4
    Last Post: 05-08-2004, 04:09 PM
  4. Binary Output
    By Ajsan in forum C++ Programming
    Replies: 9
    Last Post: 04-23-2004, 04:43 PM
  5. Array, Linked List, or Binary Tree?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 01-05-2002, 09:07 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21