iostream

Using char16_t and char32_t in I/O

荒凉一梦 提交于 2019-11-26 18:23:29
问题 C++11 introduces char16_t and char32_t to facilitate working with UTF-16- and UTF-32-encoded text strings. But the <iostream> library still only supports the implementation-defined wchar_t for multi-byte I/O. Why has support for char16_t and char32_t not been added to the <iostream> library to complement the wchar_t support? 回答1: In the proposal Minimal Unicode support for the standard library (revision 2) it is indicated that there was only support among the Library Working Group for

How to disable buffering on a stream?

依然范特西╮ 提交于 2019-11-26 18:21:45
问题 In C, I can easily set a stream to unbuffered I/O: FILE * f = fopen( "test", "r" ); setvbuf( f, (char *)NULL, _IONBF, 0 ); How would I achieve similarly unbuffered I/O using C++ IOStreams? 回答1: For file streams, you can use pubsetbuf for that : std::ifstream f; f.rdbuf()->pubsetbuf(0, 0); f.open("test"); Explanation The C++ standard says the following about the effect of setbuf (and thus pubsetbuf ) for file streams : If setbuf(0,0) is called on a stream before any I/O has occurred on that

Printing double without losing precision

点点圈 提交于 2019-11-26 17:37:58
问题 How do you print a double to a stream so that when it is read in you don't lose precision? I tried: std::stringstream ss; double v = 0.1 * 0.1; ss << std::setprecision(std::numeric_limits<T>::digits10) << v << " "; double u; ss >> u; std::cout << "precision " << ((u == v) ? "retained" : "lost") << std::endl; This did not work as I expected. But I can increase precision (which surprised me as I thought that digits10 was the maximum required). ss << std::setprecision(std::numeric_limits<T>:

How to simulate printf's %p format when using std::cout?

懵懂的女人 提交于 2019-11-26 17:13:55
问题 unsigned char *teta = ....; ... printf("data at %p\n", teta); // prints 0xXXXXXXXX How can I print variable address using iostream s? Is there a std:: ??? feature like std::hex to do this kind of conversion (address -> string), so std::cout << std::??? << teta << std::endl will print that address? (no sprintf's, please ;)) 回答1: Cast to void* : unsigned char* teta = ....; std::cout << "data at " << static_cast<void*>(teta) << "\n"; iostreams generally assume you have a string with any char*

How to detect negative numbers as parsing errors when reading unsigned integers?

人走茶凉 提交于 2019-11-26 17:12:34
问题 I want to read unsigned integers in base-10 (decimal) representation from a C++ iostream with at least rudimentary error detection. In my view, minus signs would clearly be an error in this case, because unsigned integers have no sign. However, the gcc is of a different opinion: #include <iostream> #include <sstream> int main() { std::stringstream a("5"), b("-0"), c("-4"); unsigned int i; a >> i; if ( a ) std::cout << i << std::endl; else std::cout << "Conversion failure" << std::endl; b >> i

Resetting the State of a Stream

给你一囗甜甜゛ 提交于 2019-11-26 17:04:57
问题 I have a question which is slightly similar to this question on stackoverflow std::cin.clear() fails to restore input stream in a good state, but the answer provided there does not work for me. The question is: how can I reset the state of a stream to 'good' again? Here is my code how I try it, but the state is never set to good again. I used both of the lines ignore separately. int _tmain(int argc, _TCHAR* argv[]) { int result; while ( std::cin.good() ) { std::cout << "Choose a number: ";

How to read a complete line from the user using cin?

你说的曾经没有我的故事 提交于 2019-11-26 16:43:12
Here is my current C++ code. I would like to know how to write a line of code. Would I still use cin.getline(y) or something different? I've checked, but can't find anything. When I run it, it works perfectly except it only types one word instead of the full lines I need it to output. This is what I need help with. I've outlined it in the code. Thanks for helping #include <iostream> #include <cstdlib> #include <cstring> #include <fstream> using namespace std; int main() { char x; cout << "Would you like to write to a file?" << endl; cin >> x; if (x == 'y' || x == 'Y') { char y[3000]; cout <<

Redirecting standard input of console application

三世轮回 提交于 2019-11-26 16:35:07
I have a console application which I'm trying to automate by redirecting Standard input stream of the process. In manual mode after opening the application, it waits for user input like below, I created the process with redirected Standard input stream.The code snippet is as follows, Process newProcess = new Process(); newProcess.StartInfo.FileName = exeName; newProcess.StartInfo.Arguments = argsLine; newProcess.StartInfo.UseShellExecute = false; newProcess.StartInfo.RedirectStandardOutput = false ; newProcess.StartInfo.CreateNoWindow = false; newProcess.StartInfo.RedirectStandardInput = true;

Loading a file into a vector<char>

半腔热情 提交于 2019-11-26 16:28:30
问题 I would like to load the contents of a text file into a vector<char> (or into any char input iterator, if that is possible). Currently my code looks like this: std::vector<char> vec; std::ifstream file("test.txt"); assert(file.is_open()); while (!(file.eof() || file.fail())) { char buffer[100]; file.read(buffer, 100); vec.insert(vec.end(), buffer, buffer + file.gcount()); } I do not like the manual use of a buffer (Why 100 chars? Why not 200, or 25 or whatever?), or the large number of lines

How to output a character as an integer through cout?

a 夏天 提交于 2019-11-26 16:14:50
#include <iostream> using namespace std; int main() { char c1 = 0xab; signed char c2 = 0xcd; unsigned char c3 = 0xef; cout << hex; cout << c1 << endl; cout << c2 << endl; cout << c3 << endl; } I expected the output are as follows: ab cd ef Yet, I got nothing. I guess this is because cout always treats 'char', 'signed char', and 'unsigned char' as characters rather than 8-bit integers. However, 'char', 'signed char', and 'unsigned char' are all integral types. So my question is: How to output a character as an integer through cout? PS: static_cast(...) is ugly and needs more work to trim extra