iostream

C++ buffered stream IO

冷暖自知 提交于 2019-12-09 03:20:18
问题 I understand that by default all stream IO supported by C++ is buffered. This means that data to be output is put into a buffer till it is full and then sent to the output device, similarly for input, the data is read once the buffer is empty...all this is done so that number of expensive system calls could be minimized. But how to verify this behavior in action. I mean consider the following code int main() { cout << "Hello world\n"; return 0 } Where does buffering come into picture here ? I

Tell `endl` not to flush

馋奶兔 提交于 2019-12-08 18:35:29
My program prints a large number of short lines to cout . As a slightly contrived example, my lines look a little like this: cout<<"The variable's value is: "<<variable<<endl; I'd like the program to run fast and I do believe that endl is killing me because it initiates a buffer flush on cout every time it is used. Now, some folks on the internet have said that I could do this instead: cout<<"The variable's value is: "<<variable<<"\n"; But this does not seem like a good solution because endl abstracts the particular system-specific ways an end line might be specified, where as \n does not.

Disabling pointer output in C++ streams?

被刻印的时光 ゝ 提交于 2019-12-08 16:27:44
问题 If you hand any pointer to a C++ stream, it's address will be put into the output. (Obviously unless there's a more specific output handler.) void* px = NULL; const char* ps = "Test"; FooType* pf = ...; stringstream s; s << ps << " " << px << " " << pf "\n"; s.str(); // yields, for example: "Test 0 AF120089" This can be a problem, if the user erroneously was trying to actually print the value of FooType. And it is also a problem when mixing wide char and narrow char, because instead of a

Searching for a string in an input stream

拟墨画扇 提交于 2019-12-08 16:22:54
问题 I have a large binary file (many gigabytes, so loading it into memory is not an option) that I want to search for all occurrences of the string "icpf". I tried using std::search for this, but just got bitten by the fact that std::search only works for forward iterators, not input iterators. Does the standard library provide a fast alternative for this? Or do I need to hand-code the search (either reading in chunks at a time then std::search on those, or ignore everything until an 'i' and then

clang iostream - symbol not found

﹥>﹥吖頭↗ 提交于 2019-12-08 15:44:58
问题 So I'm trying to get the clang compiler to work.. my natural first choice of program was the following extremely complex code: #include <iostream> using std::cout; using std::endl; /* hello world.cpp */ int main() { cout << "Hello, world!" << endl; return 0; } On the command line I did: clang helloworld.cpp and I get the following nice error: Undefined symbols for architecture x86_64: "std::ios_base::Init::~Init()", referenced from: ___cxx_global_var_init in cc-4iziZq.o "std::ios_base::Init:

Reference Linux username in string to open file

那年仲夏 提交于 2019-12-08 13:54:18
问题 I have a config file which contains the path to another file which needs to be opened. This file path references the Linux username: /root/${USER}/workspace/myfile.txt where $USER should be translated to the Linux username. This doesn't work and because the string is stored in my config file, I cannot use getenv() . Is there another way to achieve this? 回答1: You can use wordexp to translate "~" which is a UNIX path element meaning the HOME directory. Something like this: #include <wordexp.h>

Read Text file using Boost mmap

最后都变了- 提交于 2019-12-08 06:50:52
问题 I am reading the following file: File.txt Y:\Test\DOCUMENTS\DOCUMENTS\Flux Assurance 2\multi\ACTEPROC_OK\018-1-R.xml Y:\Test\DOCUMENTS\DOCUMENTS\Flux Assurance 2\multi\ACTEPROC_OK\A018-2-R.xml Y:\Test\DOCUMENTS\DOCUMENTS\Flux Assurance 2\multi\ACTEPROC_OK\021-1-R.xml Y:\Test\DOCUMENTS\DOCUMENTS\Flux Assurance 2\multi\ACTEPROC_OK\A021-2-R.xml Y:\Test\DOCUMENTS\DOCUMENTS\Flux Assurance 2\multi\ACTEPROC_OK\022-1-R.xml Y:\Test\DOCUMENTS\DOCUMENTS\Flux Assurance 2\multi\ACTEPROC_OK\022-2-R.xml Y:

Line number in a C++ istream?

孤街浪徒 提交于 2019-12-08 03:00:27
问题 Suppose I'm reading tokens from a std::istream . What is a good way to know the number of newlines that have been read from the stream? This is for the purpose of error reporting in a parser. I don't want to use std::getline to read input. Here is a test case. I am looking for something functionally similar to GetLineNumber , which in this case would return the line number of the last read token. std::stringstream ss; ss << "1 \n 2 \n 3 \n"; int x; while (ss >> x) { std::cout << "Line " <<

nvlink : error : Undefined reference when trying to write to text file

…衆ロ難τιáo~ 提交于 2019-12-08 03:00:19
问题 I'm getting this really bizarre (in my perspective) error in VS2012 when trying to compile some code that had previously worked. I use CUDA to generate a 2D array of data and my goal is to write it to a text file...but when I append this snippet of code from the example at the end of my main function // basic file operations #include <iostream> #include <fstream> using namespace std; int main () { ofstream myfile; myfile.open ("example.txt"); myfile << "Writing this to a file.\n"; myfile

Wrapping a commandline program with pstream

假装没事ソ 提交于 2019-12-08 01:14:15
问题 I want to be able to read and write to a program from C++. It seems like pstream can do the job, but I find the documentation difficult to understand and have not yet find an example. I have setup the following minimum working example. This opens python, which in turn (1) prints hello (2) ask input, and (3) prints hello2 : #include <iostream> #include <cstdio> #include "pstream.h" using namespace std; int main(){ std::cout << "start"; redi::pstream proc(R"(python -c "if 1: print 'hello' raw