iostream

When does cout flush?

萝らか妹 提交于 2019-12-17 14:45:48
问题 I know endl or calling flush() will flush it. I also know that when you call cin after cout , it flushes too. And also when the program exit. Are there other situations that cout flushes? I just wrote a simple loop, and I didn't flush it, but I can see it being printed to the screen. Why? Thanks! for (int i =0; i<399999; i++) { cout<<i<<"\n"; } Also the time for it to finish is same as with endl both about 7 seconds. for (int i =0; i<399999; i++) { cout<<i<<endl; } 回答1: There is no strict

Reading directly from an std::istream into an std::string

孤街醉人 提交于 2019-12-17 10:55:11
问题 Is there anyway to read a known number of bytes, directly into an std::string, without creating a temporary buffer to do so? eg currently I can do it by boost::uint16_t len; is.read((char*)&len, 2); char *tmpStr = new char[len]; is.read(tmpStr, len); std::string str(tmpStr, len); delete[] tmpStr; 回答1: std::string has a resize function you could use, or a constructor that'll do the same: boost::uint16_t len; is.read((char*)&len, 2); std::string str(len, '\0'); is.read(&str[0], len); This is

Alternative function in iostream.h for getch() of conio.h?

断了今生、忘了曾经 提交于 2019-12-17 09:55:10
问题 I'm trying to hold the screen on my output using the header file <iostream.h> , but I don't know any equivalent function to the getch() & clrscr() functions of <conio.h> in <iostream.h> or any other C++ library. Are there any such functions? 回答1: if you work on windows you can use system("pause"), this will give you "press any key to continue" message. 回答2: The conio.h functions are compiler extensions to the language, not part of C or C++. There isn't a direct replacement in standard C++.

Obtain a std::ostream either from std::cout or std::ofstream(file)

谁说我不能喝 提交于 2019-12-17 08:24:47
问题 how do I bind a std::ostream to either std::cout or to an std::ofstream object, depending on a certain program condition? Although this invalid for many reasons, I would like to achieve something that is semantically equivalent to the following: std::ostream out = condition ? &std::cout : std::ofstream(filename); I've seen some examples that are not exception-safe, such as one from http://www2.roguewave.com/support/docs/sourcepro/edition9/html/stdlibug/34-2.html: int main(int argc, char *argv

Obtain a std::ostream either from std::cout or std::ofstream(file)

荒凉一梦 提交于 2019-12-17 08:24:45
问题 how do I bind a std::ostream to either std::cout or to an std::ofstream object, depending on a certain program condition? Although this invalid for many reasons, I would like to achieve something that is semantically equivalent to the following: std::ostream out = condition ? &std::cout : std::ofstream(filename); I've seen some examples that are not exception-safe, such as one from http://www2.roguewave.com/support/docs/sourcepro/edition9/html/stdlibug/34-2.html: int main(int argc, char *argv

checking data availability before calling std::getline

試著忘記壹切 提交于 2019-12-17 07:42:09
问题 I would like to read some data from a stream I have using std::getline . Below a sample using the std::cin . std::string line; std::getline( std::cin, line ); This is a blocking function i.e. if there is no data or line to read it blocks execution. Do you know if exists a function for checking data availability before calling std::getline ? I don't want to block. How can I check whether the stream buffer is full of data valid for a successful call to std::getline ? Whatever looks like the

ostream chaining, output order

余生长醉 提交于 2019-12-17 07:37:26
问题 I have a function that takes an ostream reference as an argument, writes some data to the stream, and then returns a reference to that same stream, like so: #include <iostream> std::ostream& print( std::ostream& os ) { os << " How are you?" << std::endl; return os; } int main() { std::cout << "Hello, world!" << print( std::cout ) << std::endl; } The output of this code is: How are you? Hello, world!0x601288 However, if I separate the chaining expressions into two statements, like this int

Are int8_t and uint8_t intended to be char types?

纵然是瞬间 提交于 2019-12-17 07:23:22
问题 Given this C++11 program, should I expect to see a number or a letter? Or not make expectations? #include <cstdint> #include <iostream> int main() { int8_t i = 65; std::cout << i; } Does the standard specify whether this type can or will be a character type? 回答1: From § 18.4.1 [cstdint.syn] of the C++0x FDIS (N3290), int8_t is an optional typedef that is specified as follows: namespace std { typedef signed integer type int8_t; // optional //... } // namespace std § 3.9.1 [basic.fundamental]

What is the <iosfwd> header?

ε祈祈猫儿з 提交于 2019-12-17 07:13:31
问题 What's the <iosfwd> header used for? Why is it necessary? Any example? 回答1: It's so you can declare, in your own headers, methods that rely on the declarations of iostream types without having to #include the iostream headers themselves, which are large, complex and slow to compile against. Here's a simple example: // foo.h #include <iosfwd> void sucker(std::iostream& is); // foo.cc #include <iostream> void sucker(std::iostream& is) { is >> somevar; } 回答2: As @Marcelo Cantos mentioned, it's

Byte Stream and Character stream

China☆狼群 提交于 2019-12-17 07:07:28
问题 Please explain what Byte streams and Character streams are. What exactly do these mean? Is a Microsoft Word document Byte oriented or Character oriented? Thanks 回答1: A stream is a way of sequentially accessing a file. A byte stream access the file byte by byte. A byte stream is suitable for any kind of file, however not quite appropriate for text files. For example, if the file is using a unicode encoding and a character is represented with two bytes, the byte stream will treat these