iostream

Custom manipulator for C++ iostream

懵懂的女人 提交于 2019-11-26 09:28:43
问题 I\'d like to implement a custom manipulator for ostream to do some manipulation on the next item being inserted into the stream. For example, let\'s say I have a custom manipulator quote : std::ostringstream os; std::string name(\"Joe\"); os << \"SELECT * FROM customers WHERE name = \" << quote << name; The manipulator quote will quote name to produce: SELECT * FROM customers WHERE name = \'Joe\' How do I go about accomplishing that? Thanks. 回答1: It's particularly difficult to add a

How to make cout behave as in binary mode?

喜夏-厌秋 提交于 2019-11-26 09:14:02
问题 Every time I do \'cout << endl\' or even \'cout << \"\\n\"\' and then launch my program under Windows to output to a file (\"a.exe < test.in > result.out\") I get \"\\r\\n\" line endings in \"result.out\". Is there on earth a way to stop it doing so and just output \"\\n\" on every \'cout << \"\\n\"\'? Thanks in advance. 回答1: This works using Visual Studio 2013: #include <io.h> #include <fcntl.h> #include <iostream> int main( int argc, char * argv[] ) { _setmode( _fileno( stdout ), _O_BINARY

Why does std::cout convert volatile pointers to bool?

我们两清 提交于 2019-11-26 09:04:35
问题 If you try to cout a pointer to a volatile type, even a volatile char pointer where you would normally expect cout to print the string, you will instead simply get \'1\' (assuming the pointer is not null I think). I assume output stream operator<< is template specialized for volatile pointers, but my question is, why? What use case motivates this behavior? Example code: #include <iostream> #include <cstring> int main() { char x[500]; std::strcpy(x, \"Hello world\"); int y; int *z = &y; std:

How to write custom input stream in C++

时光毁灭记忆、已成空白 提交于 2019-11-26 08:48:49
问题 I\'m currently learning C++ (Coming from Java) and I\'m trying to understand how to use IO streams properly in C++. Let\'s say I have an Image class which contains the pixels of an image and I overloaded the extraction operator to read the image from a stream: istream& operator>>(istream& stream, Image& image) { // Read the image data from the stream into the image return stream; } So now I\'m able to read an image like this: Image image; ifstream file(\"somepic.img\"); file >> image; But now

Find all a substring&#39;s occurrences and locations

谁说我不能喝 提交于 2019-11-26 08:27:22
问题 I\'m writing a program to parse some data saved as text files. What I am trying to do is find the location of every needle in a haystack. I already can read the file in and determine the number of occurrences, but I am looking to find the index also. 回答1: string str,sub; // str is string to search, sub is the substring to search for vector<size_t> positions; // holds all the positions that sub occurs within str size_t pos = str.find(sub, 0); while(pos != string::npos) { positions.push_back

Reading and writing to the same file using the same fstream

大城市里の小女人 提交于 2019-11-26 07:42:34
问题 I have a file that already contains some data (say, 8 kB). I want to read something from the beginning of the file, and then overwrite data starting where I finished reading. So I try to use the following code: std::fstream stream(\"filename\", std::ios::in | std::ios::out | std::ios::binary); char byte; stream.read(&byte, 1); // stream.seekp(1); int bytesCount = 4096; auto bytesVec = std::vector<char>(bytesCount, \'c\'); char* bytes = bytesVec.data(); std::cout << stream.bad() << std::endl;

Can I use CreateFile, but force the handle into a std::ofstream?

空扰寡人 提交于 2019-11-26 05:38:51
问题 Is there any way to take advantage of the file creation flags in the Win32 API such as FILE_FLAG_DELETE_ON_CLOSE or FILE_FLAG_WRITE_THROUGH as described here http://msdn.microsoft.com/en-us/library/aa363858(VS.85).aspx , but then force that handle into a std::ofstream? The interface to ofstream is obviously platform independent; I\'d like to force some platform dependent settings in \'under the hood\' as it were. 回答1: It is possible to attach a C++ std::ofstream to a Windows file handle. The

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

独自空忆成欢 提交于 2019-11-26 04:56:00
问题 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 << \

How to output a character as an integer through cout?

只谈情不闲聊 提交于 2019-11-26 03:55:43
问题 #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 read line by line or a whole text file at once?

前提是你 提交于 2019-11-26 03:49:41
问题 I\'m in a tutorial which introduces files (how to read and write from\\to file) First of all, this is not a homework, this is just general help I\'m seeking. I know how to read one word at a time, but I don\'t know how to read one line at a time or how to read the whole text file. What if my file contains 1000 words? It is not practical to read each word. My text file named (Read) contains the following: I love to play games I love reading I have 2 books This is what I have accomplished so