iostream

Why is std::streamsize defined as signed rather than unsigned?

依然范特西╮ 提交于 2020-11-30 07:56:19
问题 According to http://en.cppreference.com/w/cpp/io/streamsize The type std::streamsize is a signed integral type used to represent the number of characters transferred in an I/O operation or the size of an I/O buffer. As far as I can imagine, a stream's size can never be negative, so, my question is: Why is std::streamsize defined as signed rather than unsigned ? What's the rationale behind? 回答1: The draft C++ standard has the following footnote 296 in section 27.5.2 Types which says:

Portable printing of exponent of a double to C++ iostreams

让人想犯罪 __ 提交于 2020-06-24 07:19:11
问题 I want to print a double value to std::cout portably (GCC, clang, MSVC++) such that the output is the same on all platforms. I have a problem with the formatting of the exponent. The following program #include <iostream> int main() { std::cout << 0.1e-7 << std::endl; return 0; } Has this output with GCC: 1e-08 and the following output with MSVC 1e-008 How can I make both outputs the same? I'm sorry if this is a dumb question but I have not found an answer so far. All formatting seems to

How to fix “fatal error: 'iostream' file not found” after upgrading to Xcode 10.1

*爱你&永不变心* 提交于 2020-05-28 12:02:13
问题 This error has only appeared for me since updating Xcode (and to MacOS Mojave 10.14). Something similar happened with #include <Python> , which I fixed by instead using #include "python2.7/Python.h". There is a similar error discussed in Clang doesn't see basic headers. When I try clang++ -stdlib=libc++ PyTrans.cpp -o -v I get ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) . The full error: warning: include path

How to pipe into std::cout with boost::iostreams

孤者浪人 提交于 2020-05-23 09:52:49
问题 I am new to boost::iostreams so this might be trivial: Assuming namespace io = boost::iostreams; this works io::filtering_ostream out(std::cout); out << "some\nstring\n"; and this works std::string result; io::filtering_ostream out(io::counter() | io::back_inserter(result)); out << "some\nstring\n"; yet this does not compile io::filtering_ostream out(io::counter() | std::cout); out << "some\nstring\n"; How do you pipe into std::cout ? 回答1: Wrapping std::cout with boost::ref worked for me: io:

How to clear the state bits in an iostream object in C++?

☆樱花仙子☆ 提交于 2020-04-30 07:49:11
问题 I'm trying to learn C++ from an older edition of the Primer, and tried to execute some of their code relating to iostream objects, which gave me some trouble: #include <iostream> #include <cstdlib> using namespace std; int main(int argc, char *argv[]) { int ival; try { while (cin >> ival, !cin.eof()) { if (cin.bad()) throw runtime_error("IO stream corrupted"); if (cin.fail()) { cerr << "Invalid input - try again"; cin.clear(iostream::failbit); continue; } else cout << ival << endl; } return

How to clear the state bits in an iostream object in C++?

早过忘川 提交于 2020-04-30 07:48:16
问题 I'm trying to learn C++ from an older edition of the Primer, and tried to execute some of their code relating to iostream objects, which gave me some trouble: #include <iostream> #include <cstdlib> using namespace std; int main(int argc, char *argv[]) { int ival; try { while (cin >> ival, !cin.eof()) { if (cin.bad()) throw runtime_error("IO stream corrupted"); if (cin.fail()) { cerr << "Invalid input - try again"; cin.clear(iostream::failbit); continue; } else cout << ival << endl; } return