iostream

Performance problems when scaling MSVC 2005's operator<< accross threads.

When looking at some of our logging I've noticed in the profiler that we were spending a lot of time in the operator<< formatting ints and such. It looks like there is a shared lock that is used whenever ostream::operator<< is called when formatting an int(and presumably doubles). Upon further investigation I've narrowed it down to this ...

Alternative function in iostream.h for getch() of conio.h?

I'm trying to hold the screen on my output using the header file <iostream.h>, but I don't know any equivalent function to the getch() & clrscr() functions of <conio.h> in <iostream.h> or any other C++ library. Are there any such functions? ...

How do you output variable's declared as a double to a text file in C++

I am very new to C++ and I am wondering how you output/write variables declared as double to a txt file. I know about how to output strings using fstream but I cant figure out how to send anything else. I am starting to think that you can't send anything but strings to a text file is that correct? If so then how would you convert the inf...

Is it ok to use wcout to print char* ?

Consider this line: std::wcout << "Hello World!"; Is it OK to pass char* or char to wide stream? ...

How do I use basic_filebuf with element type other than char?

Say I want to read the contents of a file using basic_filebuf. I have a type called boost::uintmax_t which has a size of 8 bytes. I am trying to write the following: typedef basic_filebuf<uintmax_t> file; typedef istreambuf_iterator<uintmax_t> ifile; file f; vector<uintmax_t> data, buf(2); f.open("test.txt", std::ios::in | std::ios::b...

Sharing output streams through a JNI interface

I am writing a Java application that uses a C++ library through a JNI interface. The C++ library creates objects of type Foo, which are duly passed up through JNI to Java. Suppose the library has an output function void Foo::print(std::ostream &os) and I have a Java OutputStream out. How can I invoke Foo::print from Java so that...

How can I make my char buffer more performant?

I have to read a lot of data into: vector<char> A 3rd party library reads this data in many turns. In each turn it calls my callback function whose signature is like this: CallbackFun ( int CBMsgFileItemID, unsigned long CBtag, void* CBuserInfo, int CBdataSize, void* CBdataBuffe...

Are standard output streams in C++ thread-safe (cout, cerr, clog)?

I know that there is no concept of threads in current C++, but this article is saying: A typesafe, threadsafe, portable logging mechanism ..... The fprintf() function is threadsafe, so even if this log is used from different threads, the output lines won't be scrambled. What about cout, cerr and clog? I think thi...

stringstream temporary ostream return problem

I'm creating a logger with the following sections: // #define LOG(x) // for release mode #define LOG(x) log(x) log(const string& str); log(const ostream& str); With the idea to do: LOG("Test"); LOG(string("Testing") + " 123"); stringstream s; LOG(s << "Testing" << 1 << "two" << 3); This all works as intended, but when I do: LOG(s...

Are there binary memory streams in C++

Hello, I usually use stringstream to write into in-memory string. Is there a way to write to a char buffer in binary mode? Consider the following code: stringstream s; s << 1 << 2 << 3; const char* ch = s.str().c_str(); The memory at ch will look like this: 0x313233 - the ASCII codes of the characters 1, 2 and 3. I'm looking for a wa...

Are large include files like iostream efficient? (C++)

Iostream, when all of the files it includes, the files that those include, and so on and so forth, adds up to about 3000 lines. Consider the hello world program, which needs no more functionality than to print something to the screen: #include <iostream> //+3000 lines right there. int main() { std::cout << "Hello, World!"; retu...

Can you tell iostreams which characters to treat as whitespace?

So that you could do something like this, for instance: std::string a("01:22:42.18"); std::stringstream ss(a); int h, m, s, f; ss >> h >> m >> s >> f; Which normally requires the string to be formatted "01 22 42 18". Can you modify the current locale directly to do this? ...

proper way to read user input from command line in java

I was hoping to get some opinions regarding best practices and comments on the way I read user input from the command line. Is there a recommended way to do this, am I using the try/catch blocks properly? My example here works fine, but would still like to hear if there is a 'cleaner' way to do this. Many thanks. For example are he retu...

How do I get the InputStream of decompressed data from an InputStream of GZIPed data?

I call a service which returns a gzipped file. I have the data as an InputStream (courtesy of javax.activation.DataHandler.getInputStream();) from the response. What I would like to do is, without writing anything to disk, get an InputStream of the decompressed data in the file that is in the archive. The compressed file in this case i...

Compose output streams

I'd like to compose two (or more) streams into one. My goal is that any output directed to cout, cerr, and clog also be outputted into a file, along with the original stream. (For when things are logged to the console, for example. After closing, I'd like to still be able to go back and view the output.) I was thinking of doing somethin...

std::ostream not formatting const char* correctly the first time it's used

I've been writing a custom std::streambuf as part of a logging system. However, I'm having problems with the first piece of output from a stream not being formatted correctly. Here's a reduced test-case that doesn't use any custom streambuf or ostream classes: #include <iostream> int main() { std::streambuf *coutbuf = std::cout.rd...

Reading directly from an std::istream into an std::string

Is there anyway to read a known number of bytes, directly into an std::string, without creating a temporary buffer to do so? eg currently I can do it by boost::uint16_t len; is.read((char*)&len, 2); char *tmpStr = new char[len]; is.read(tmpStr, len); std::string str(tmpStr, len); delete[] tmpStr; ...

#include iostream in C?

In C++ we always put the following at the top of the program #include <iostream> What about for C? ...

Why does `include <iostream>` end up including so *many* files?

Follow up of this question: When I do include <iostream> . It happens that it includes many files from /usr/include .A grep "\usr\include" over g++ -E prog.cpp counted to about 1260 entries ;). Is their a way to control including various files? Platform: Linux G++ version: 4.2.4 ...

C++ Overloading the >> operator

I need to overload the stream extraction operator. I need to do this by allowing a user to input a string of characters at a prompt, say "iamastring", and then the operator would extract each character from the string and test whether or not it is whitespace and if it is not whitespace store it in a character array which is then passed ...