iostream

What does the “c” mean in cout, cin, cerr and clog?

流过昼夜 提交于 2019-11-30 02:39:43
What does the "c" mean in the cout, cin, cerr and clog names? I would say char but I haven't found anything to confirm it. fredoverflow The "c" stands for "character" because iostreams map values to and from byte (char) representations. [ Bjarne Stroustrup's C++ Style and Technique FAQ ] JRL I originally guessed console , and this link confirmed it. But after seeing the quote from Stroustrup , it seems that's a misconception, and that the c stands for character . One thing in favor of that theory that can serve as an indicator is the fact that for each stream object (cin, cout, cerr, etc.)

The difference between cin.ignore and cin.sync

放肆的年华 提交于 2019-11-30 02:23:30
What is the difference between cin.ignore and cin.sync ? cin.ignore discards characters, up to the number specified, or until the delimiter is reached (if included). If you call it with no arguments, it discards one character from the input buffer. For example, cin.ignore (80, '\n') would ignore either 80 characters, or as many as it finds until it hits a newline. cin.sync discards all unread characters from the input buffer. However, it is not guaranteed to do so in each implementation. Therefore, ignore is a better choice if you want consistency. cin.sync() would just clear out what's left.

Does anyone actually use stream extraction operators?

假装没事ソ 提交于 2019-11-30 01:45:37
I've written tons of operator<<(std::ostream &, const T &) functions -- they're incredibly useful. I've never written an operator>>(std::istream &, T &) function in real code or even used the extraction operators for built-in types (OK, maybe for std::string ). Are these appropriate only for short example programs and textbooks? Is operator>> a failed feature of C++? Questions have been asked about safely overloading stream operators . What I wonder is if anyone does this in practice. Even for something simple like reading input from a file in C++ I can't suggest using operator>> . It's too

c++ multiple definitions of operator<<

☆樱花仙子☆ 提交于 2019-11-30 01:35:06
I am attempting to override the << operator for a class. The purpose is basically to implement a toString() like behavior for my class, so that sending it to cout will produce useful output. Using a dummy example, I have the code below. When I attempt to compile, I get the foollowing error: $ g++ main.cpp Rectangle.cpp /tmp/ccWs2n6V.o: In function `operator<<(std::basic_ostream<char, std::char_traits<char> >&, CRectangle const&)': Rectangle.cpp:(.text+0x0): multiple definition of `operator<<(std::basic_ostream<char, std::char_traits<char> >&, CRectangle const&)' /tmp/ccLU2LLE.o:main.cpp:(.text

How can I read line-by-line using Boost IOStreams' interface for Gzip files?

淺唱寂寞╮ 提交于 2019-11-30 00:25:22
I managed to integrate the boost Iostream APIs for reading zipped files. I followed the documentation in boost page and have the following code so-far: std::stringstream outStr; ifstream file("file.gz", ios_base::in | ios_base::binary); try { boost::iostreams::filtering_istreambuf in; in.push(boost::iostreams::gzip_decompressor()); in.push(file); boost::iostreams::copy(in, outStr); } catch(const boost::iostreams::gzip_error& exception) { int error = exception.error(); if (error == boost::iostreams::gzip::zlib_error) { //check for all error code } } The code works fine (so please ignore any

What serious alternatives exist for the IOStream library? (besides cstdio)

给你一囗甜甜゛ 提交于 2019-11-29 20:51:15
I'm looking for a library which operates similar to iostreams, in that it performs conversions, and allows writing to memory buffers, files, and the console. However, I'd like something type safe, as iostream is. Are there any serious libraries which do this? Being able to specify the output encoding for things would be a plus. Note that I'm not interested in libraries which simply front iostreams because they just add more complexity to what iostreams is doing, e.g. boost::format . PreEmptive comment response: I don't want to use cstdio because using that system it's impossible to have code

Converting ostream into standard string

旧城冷巷雨未停 提交于 2019-11-29 20:23:34
I am very new to the C++ STL, so this may be trivial. I have a ostream variable with some text in it. ostream* pout; (*pout) << "Some Text"; Is there a way to extract the stream and store it in a string of type char* ? James Curran std::ostringstream stream; stream << "Some Text"; std::string str = stream.str(); const char* chr = str.c_str(); And I explain what's going on in the answer to this question , which I wrote not an hour ago. Foo The question was on ostream to string, not ostringstream to string. For those interested in having the actual question answered (specific to ostream ), try

Why use endl when I can use a newline character? [duplicate]

扶醉桌前 提交于 2019-11-29 19:40:11
This question already has an answer here: C++: “std::endl” vs “\n” 13 answers Is there a reason to use endl with cout when I can just use \n ? My C++ book says to use endl, but I don't see why. Is \n not supported as widely as endl , or am I missing something? endl appends '\n' to the stream and calls flush() on the stream. So cout << x << endl; is equivalent to cout << x << '\n'; cout.flush(); A stream may use an internal buffer which gets actually streamed when the stream is flushed. In case of cout you may not notice the difference since it's somehow synchronized ( tied ) with cin , but for

Are standard output streams in C++ thread-safe (cout, cerr, clog)?

旧巷老猫 提交于 2019-11-29 18:18:40
问题 I know that there is no concept of threads in current C++ , but this article is saying: A typesafe, threadsafe, portable logging mechanism ..... The fprintf() function is threadsafe, so even if this log is used from different threads, the output lines won't be scrambled. What about cout , cerr and clog ? I think this question is applicable to all kind of stream types in C++ also, like fstream and stringstream . 回答1: The article makes a claim about the POSIX standard for the fprintf API. It

Why is it that wcout << “”; is OK but wcout << string(); is not?

ⅰ亾dé卋堺 提交于 2019-11-29 16:57:03
问题 #include <iostream> #include <string> using namespace std; int main() { wcout << L"Hello"; // OK. wcout << wstring(L"Hello"); // OK. wcout << "Hello"; // OK. Why? wcout << string("Hello"); // Error. Why? } Why does std::wcout accept a narrow string literal as its argument but doesn't accept a narrow string object? 回答1: This is dictated by § 27.7.3.6.4 of the C++11 Standard, where the following two overloaded operators (among others) are specified: template<class charT, class traits> basic