iostream

Add time stamp with std::cout

你离开我真会死。 提交于 2019-11-28 09:29:07
I have the following code which is redirecting my std::cout output to a log file. std::ofstream out("out.txt"); std::streambuf *coutbuf = std::cout.rdbuf(); //save old buf std::cout.rdbuf(out.rdbuf()); //redirect std::cout to out.txt! Now what I want is that whenever a newline is occurring, then the current time stamp will be written to the file. I know I can achive this with: std::cout << getTime() << "printing data" << std::endl; But what I want is that of std::cout taking care of it automatically somehow. Is that possible? I assume, that You want print the TimeStamp, if the first character

How can I read from memory just like from a file using iostream?

*爱你&永不变心* 提交于 2019-11-28 08:45:33
I have simple text file loaded into memory. I want to read from memory just like I would read from a disc like here: ifstream file; string line; file.open("C:\\file.txt"); if(file.is_open()) { while(file.good()) { getline(file,line); } } file.close(); But I have file in memory. I have an address in memory and a size of this file. What I must do to have the same fluency as with dealing with file in the code above? You can do something like the following.. std::istringstream str; str.rdbuf()->pubsetbuf(<buffer>,<size of buffer>); And then use it in your getline calls... NOTE: getline does not

C++ Input Performance

倾然丶 夕夏残阳落幕 提交于 2019-11-28 07:45:24
问题 I was trying to solve a problem on InterviewStreet. After some time I determine that I was actually spending the bulk of my time reading the input. This particular question had a lot of input, so that makes some amount of sense. What doesn't make sense is why the varying methods of input had such different performances: Initially I had: std::string command; std::cin >> command; Replacing it made it noticeably faster: char command[5]; cin.ignore(); cin.read(command, 5); Rewriting everything to

Misunderstanding about ostream class and operator <<

陌路散爱 提交于 2019-11-28 07:17:55
问题 After looking at the ostream::operator << c++ reference, I noticed the following declarations: ostream& operator<< (bool val); ostream& operator<< (short val); ostream& operator<< (unsigned short val); ostream& operator<< (int val); ostream& operator<< (unsigned int val); ostream& operator<< (long val); ostream& operator<< (unsigned long val); ostream& operator<< (float val); ostream& operator<< (double val); ostream& operator<< (long double val); ostream& operator<< (void* val); ostream&

C++: assign cin to an ifstream variable?

a 夏天 提交于 2019-11-28 07:02:37
问题 You know the common stdio idiom that stdin is specified by a filename of "-", e.g. if ((strcmp(fname, "-")) fp = fopen(fname); else fp = stdin; What's the best way to do this with an ifstream instance? I've received a bit of code that has an ifstream as part of a class and I'd like to add code to do the equivalent, something like: if ( filename == "-") logstream = cin; // **how do I do this*?* else logstream.open( filename.c_str() ); 回答1: cin is not an ifstream , but if you can use istream

redirect std::cout to a custom writer

为君一笑 提交于 2019-11-28 06:38:44
I want to use this snippet from Mr-Edd's iostreams article to print std::clog somewhere. #include <iostream> #include <iomanip> #include <string> #include <sstream> int main() { std::ostringstream oss; // Make clog use the buffer from oss std::streambuf *former_buff = std::clog.rdbuf(oss.rdbuf()); std::clog << "This will appear in oss!" << std::flush; std::cout << oss.str() << '\\n'; // Give clog back its previous buffer std::clog.rdbuf(former_buff); return 0; } so, in a mainloop, I will do something like while (! oss.eof()) { //add to window text somewhere } Here's the ostringstream docs but

How to easily make std::cout thread-safe?

那年仲夏 提交于 2019-11-28 04:43:20
I have a multi-threaded application, which heavily uses std::cout for logging without any locking. In such a case, how can I easily add lock mechanism to make std::cout thread-safe? I don't want to search for each occurrence of std::cout and add a line of locking code. That is too tedious. Any better practice? Note: This answer is pre-C++20 so it does not use std::osyncstream with its separate buffering, but uses a lock instead. I guess you could implement your own class which wraps cout and associates a mutex with it. The operator << of that new class would do three things: create a lock for

C++ Streams vs. C-style IO?

橙三吉。 提交于 2019-11-28 04:32:46
I was coding some C++ for a small hobby project when I noticed that I'm using C-style operations to access IO ( printf , fopen , etc.). Is it considered "bad practice" to involve C functions in C++ projects? What are the advantages of using streams over C-style IO access? This is an heated topic. Some people prefer to use the C++ IO since they are type-safe (you can't have divergence between the type of the object and the type specified in the format string), and flow more naturally with the rest of the C++ way of coding. However, there is also arguments for C IO functions (my personal

How to create a boost ssl iostream?

佐手、 提交于 2019-11-28 04:23:36
I'm adding HTTPS support to code that does input and output using boost tcp::iostream (acting as an HTTP server). I've found examples (and have a working toy HTTPS server) that do SSL input/output using boost::asio::read/boost::asio::write, but none that use iostreams and the << >> operators. How do I turn an ssl::stream into an iostream? Working code: #include <boost/asio.hpp> #include <boost/asio/ssl.hpp> #include <boost/foreach.hpp> #include <iostream> #include <sstream> #include <string> using namespace std; using namespace boost; using boost::asio::ip::tcp; typedef boost::asio::ssl:

What is the difference between cout, cerr, clog of iostream header in c++? When to use which one?

↘锁芯ラ 提交于 2019-11-28 02:50:48
I tried researching the difference between cout , cerr and clog on the internet but couldn't find a perfect answer. I still am not clear on when to use which. Can anyone explain to me, through simple programs and illustrate a perfect situation on when to use which one? I visited this site which shows a small program on cerr and clog , but the output obtained over there can also be obtained using cout . So, I'm confused over each one's exact use. riv stdout and stderr are different streams, even though they both refer to console output by default. Redirecting (piping) one of them (e.g. program