Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make cout behave as in binary mode?

Tags:

c++

iostream

cout

Every time I do 'cout << endl' or even 'cout << "\n"' and then launch my program under Windows to output to a file ("a.exe < test.in > result.out") I get "\r\n" line endings in "result.out".

Is there on earth a way to stop it doing so and just output "\n" on every 'cout << "\n"'?

Thanks in advance.

like image 288
lithuak Avatar asked Apr 13 '11 18:04

lithuak


People also ask

Is cout part of Iostream?

The cout object is used to display the output to the standard output device. It is defined in the iostream header file.

Why Cout is not working?

There is no std::cout in C. In a windowing system, the std::cout may not be implemented because there are windows and the OS doesn't know which one of your windows to output to. never ever give cout NULL. it will stop to work.

How does STD cout work?

std::cout is used to output a value (cout = character output) std::cin is used to get an input value (cin = character input) << is used with std::cout, and shows the direction that data is moving (if std::cout represents the console, the output data is moving from the variable to the console).


1 Answers

This works using Visual Studio 2013:

#include <io.h>
#include <fcntl.h>
#include <iostream>

int main( int argc, char * argv[] )
{
    _setmode( _fileno( stdout ),  _O_BINARY );
    std::cout << std::endl;
}

It will output only [0A], and not [0D][0A].

like image 98
Rob K Avatar answered Oct 05 '22 10:10

Rob K