cout

How to make cout behave as in binary mode?

喜夏-厌秋 提交于 2019-11-26 09:14:02
问题 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. 回答1: 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

undefined reference to &#39;std::cout&#39;

纵饮孤独 提交于 2019-11-26 08:46:39
问题 Shall this be the example: #include <iostream> using namespace std; int main() { cout << \"Hola, moondo.\\n\"; } It throws the error: gcc -c main.cpp gcc -o edit main.o main.o: In function `main\': main.cpp:(.text+0xa): undefined reference to `std::cout\' main.cpp:(.text+0xf): undefined reference to `std::basic_ostream<char,std::char_traits<char> >& std::operator<< <std::char_traits<char>>(std::basic_ostream<char, std::char_traits<char> >&, char const*)\' main.o: In function `__static

Floating point format for std::ostream

北城以北 提交于 2019-11-26 04:46:55
问题 How do I do the following with std::cout? double my_double = 42.0; char str[12]; printf_s(\"%11.6lf\", my_double); // Prints \" 42.000000\" I am just about ready to give up and use sprintf_s. More generally, where can I find a reference on std::ostream formatting that lists everything in one place, rather than spreading it all out in a long tutorial? EDIT Dec 21, 2017 - See my answer below. It uses features that were not available when I asked this question in 2012. 回答1: std::cout << std:

Why I cannot cout a string?

这一生的挚爱 提交于 2019-11-26 04:20:24
问题 Why I cannot cout string like this: string text ; text = WordList[i].substr(0,20) ; cout << \"String is : \" << text << endl ; When I do this, I get the following error: Error 2 error C2679: binary \'<<\' : no operator found which takes a right-hand operand of type \'std::string\' (or there is no acceptable conversion) c:\\users\\mollasadra\\documents\\visual studio 2008\\projects\\barnamec\\barnamec\\barnamec.cpp 67 barnamec** It is amazing, that even this is not working: string text ; text

Why does streaming a char pointer to cout not print an address?

南笙酒味 提交于 2019-11-26 04:01:24
问题 When I print a char pointer with printf() , it makes the decision with conversion specifier whether the address should be printed or the whole string according to %u or %s. But when I want to do the same thing with cout , how will cout decide what should be printed among address and whole string? Here is an example source: int main() { char ch=\'a\'; char *cptr=&ch; cout<<cptr<<endl; return 0; } Here, in my GNU compiler, cout is trying to output ch as a string. How I can get address of ch via

C++ alignment when printing cout <<

孤街浪徒 提交于 2019-11-26 03:35:55
问题 Is there a way to align text when printing using std::cout ? I\'m using tabs, but when the words are too big they won\'t be aligned anymore. Sales Report for September 15, 2010 Artist Title Price Genre Disc Sale Tax Cash Merle Blue 12.99 Country 4% 12.47 1.01 13.48 Richard Music 8.49 Classical 8% 7.81 0.66 8.47 Paula Shut 8.49 Classical 8% 7.81 0.72 8.49 回答1: The ISO C++ standard way to do it is to #include <iomanip> and use io manipulators like std::setw. However, that said, those io

&#39;printf&#39; vs. &#39;cout&#39; in C++

给你一囗甜甜゛ 提交于 2019-11-26 03:15:44
问题 What is the difference between printf() and cout in C++? 回答1: I'm surprised that everyone in this question claims that std::cout is way better than printf , even if the question just asked for differences. Now, there is a difference - std::cout is C++, and printf is C (however, you can use it in C++, just like almost anything else from C). Now, I'll be honest here; both printf and std::cout have their advantages. Real differences Extensibility std::cout is extensible. I know that people will

mixing cout and printf for faster output

≡放荡痞女 提交于 2019-11-26 02:50:45
问题 After performing some tests I noticed that printf is much faster than cout . I know that it\'s implementation dependent, but on my Linux box printf is 8x faster. So my idea is to mix the two printing methods: I want to use cout for simple prints, and I plan to use printf for producing huge outputs (typically in a loop). I think it\'s safe to do as long as I don\'t forget to flush before switching to the other method: cout << \"Hello\" << endl; cout.flush(); for (int i=0; i<1000000; ++i) {

Why is address of char data not displayed?

倾然丶 夕夏残阳落幕 提交于 2019-11-26 00:17:05
问题 class Address { int i ; char b; string c; public: void showMap ( void ) ; }; void Address :: showMap ( void ) { cout << \"address of int :\" << &i << endl ; cout << \"address of char :\" << &b << endl ; cout << \"address of string :\" << &c << endl ; } The output is: address of int : something address of char : // nothing, blank area, that is nothing displayed address of string : something Why? Another interesting thing: if int, char, string is in public, then the output is ... int :

How can I pad an int with leading zeros when using cout << operator?

ぃ、小莉子 提交于 2019-11-25 23:46:10
问题 I want cout to output an int with leading zeros, so the value 1 would be printed as 001 and the value 25 printed as 025 . How can I do this? 回答1: With the following, #include <iomanip> #include <iostream> int main() { std::cout << std::setfill('0') << std::setw(5) << 25; } the output will be 00025 setfill is set to the space character ( ' ' ) by default. setw sets the width of the field to be printed, and that's it. If you are interested in knowing how the to format output streams in general,