iostream

Why does std::setprecision(6) stream more than six digits in fixed-width mode?

喜你入骨 提交于 2019-12-04 10:05:38
The output of the following code: #include <limits> #include <iostream> #include <iomanip> #include <limits> #include <string> #include <sstream> using namespace std; inline string lexical_cast(const float arg) { stringstream ss; ss << fixed << setprecision(numeric_limits<float>::digits10) << arg; if (!ss) throw "Conversion failed"; return ss.str(); } int main() { cout << numeric_limits<float>::digits10 << '\n'; cout << lexical_cast(32.123456789) << '\n'; } is: 6 32.123455 I expected, and wanted: 6 32.1234 because, to the best of my knowledge, that's the extent of what a float can reliably

Are std::showbase and std::showpos mutually exclusive?

一个人想着一个人 提交于 2019-12-04 08:17:36
This question arose from a discussion I was having about the correct way to output a numeric value using the usual ostream & operator << (ostream &, some_type) for a numeric type in C++. The way I'm familiar with the behavior of std::showbase and std::showpos in each base, they are basically mutually exclusive. That is: in decimal no base is shown, and the '+' is added on positive numbers; whereas in hexadecimal or octal, the base is shown, but a '+' is not shown (nor is a minus), as the value of the type is printed as if it's cast to an unsigned type. For example, this simple (verbose)

Binary version of iostream

时间秒杀一切 提交于 2019-12-04 07:55:36
问题 I've been writing a binary version of iostreams. It essentially allows you to write binary files, but gives you much control over the format of the file. Example usage: my_file << binary::u32le << my_int << binary::u16le << my_string; Would write my_int as a unsigned 32-bit integer, and my_string as a length-prefixed string (where the prefix is u16le.) To read the file back, you would flip the arrows. Works great. However, I hit a bump in the design, and I'm still on the fence about it. So,

Reassigning global $stdout to console - ruby

你离开我真会死。 提交于 2019-12-04 07:50:47
I am trying to set $stdout to write to a file temporarily and then back to a file. test.rb : old_stdout = $stdout $stdout.reopen("mytestfile.out",'w+') puts "this goes in mytestfile" $stdout= old_stdout puts "this should be on the console" $stdout.reopen("mytestfile1.out",'w+') puts "this goes in mytestfile1:" $stdout = old_stdout puts "this should be back on the console" Here is the output. ruby test.rb => no output on the console cat mytestfile.out this goes in mytestfile this should be on the console cat mytestfile1.out this goes in mytestfile1: this should be back on the console I am not

Reading UTF-8 text and converting to UTF-16 using standard C++ wifstream

大城市里の小女人 提交于 2019-12-04 07:36:51
I'd like to read some text from a file that uses UTF-8 encoding and convert it to UTF-16, using std::wifstream , something like this: // // Read UTF-8 text and convert to UTF-16 // std::wifstream src; src.imbue(std::locale("???")); // UTF-8 ??? src.open("some_text_file_using_utf8"); std::wstring line; // UTF-16 string while (std::getline(src, line)) { ... do something processing the UTF-16 string ... } Is there a standard locale name for the UTF-8 conversion? Is it possible to achieve that goal using std::locale ? I'm using Visual Studio 2013. NOTE: I know that I/O streams tend to be slow, and

boost::iostream::copy(), inputstream and outstream output explanantion

删除回忆录丶 提交于 2019-12-04 06:57:00
问题 I have a txt file: gcc-4.7.2.txt : with the data written: Hello This is a test file. Thanks :compressed as gcc-4.7.2.tar.bz2 Now, I run the following code: #include <sstream> #include <fstream> #include <iostream> #include <boost/iostreams/filtering_streambuf.hpp> #include <boost/iostreams/copy.hpp> #include <boost/iostreams/filter/bzip2.hpp> #include <boost/filesystem.hpp> int main() { using namespace std; using namespace boost::iostreams; char filename[] = "gcc-4.7.2.tar.bz2"; if (!boost:

inheriting ostream and streambuf problem with xsputn and overflow

不想你离开。 提交于 2019-12-04 05:30:13
I have been doing research on creating my own ostream and along with that a streambuf to handle the buffer for my ostream. I actually have most of it working, I can insert (<<) into my stream and get strings no problem. I do this by implimenting the virtual function xsputn. However if I input (<<) a float or an int to the stream instead of a string xsputn never gets called. I have walked through the code and I see that the stream is calling do_put, then f_put which eventually tries to put the float 1 character at a time into the buffer. I can get it to call my implementation of the virtual

std::cin.clear() fails to restore input stream in a good state

跟風遠走 提交于 2019-12-04 04:55:19
问题 In order to test bool i/o, I tried to run this short program: #include <iostream> int main() { while(true) { bool f; if (std::cin >> f) std::cout << f << '\n'; else { std::cout << "i/o error\n"; std::cin.clear(); } } return 0; } Here is the output I get: g++ -Wall -ansi -pedantic -o boolio boolio.cpp ./boolio 0 0 1 1 2 i/o error - i/o error t i/o error i/o error i/o error ... (infinite loop) I wonder why I get an infinite loop when I enter 't', and how to prevent it. Thanks. 回答1: Add this

ios::nocreate error while compiling a C++ code

╄→尐↘猪︶ㄣ 提交于 2019-12-04 04:16:52
问题 While, compiling a package, written in C++ on RHEL 5.0. I am getting the following error. > error: nocreate is not a member of std::ios The source-code corresponds to: ifstream tempStr( argv[4] , ios::in | ios::nocreate ); I have tried #g++ -O -Wno-deprecated <file.cpp> -o <file> as well as: #g++ -O -o <file> Please suggest a solution. 回答1: ios::nocreate is not part of standard C++ - what are you expecting it to do? Edit: From a Google, it seems like it was intended to prevent the file being

redirect std::cout to QTextEdit

自闭症网瘾萝莉.ら 提交于 2019-12-04 04:15:33
问题 Is it possible (and more importantly -how-) to redirect an output stream to a QTextBox. So that if I write std::cout << "test" anywhere in the application it gets redirected to a textbox I defined? I tried the obvious (where ui.textEdit is a pointer to the text edit box): std::cout.rdbuf(ui.textEdit); std::cout << "test"; However this doesn't work. (obviously). - Nor does redirecting cout to qDebug work (or even direction a qDebug to a textfield). I'm using qt4.8 btw... EDIT: So I tried the