iostream

Using RAII with C++ streams and STL containers?

I'm trying to use RAII concepts with an STL container of ofstream objects. For example: int main(int argc, char**argv) { std::deque<std::ofstream> sList; sList.push_back(std::ofstream()); // tried variations such as *(new ofstream()) sList[0].open("test1.txt"); sList[0] << "This is a test"; sList[0].close(); } However, no ...

C++ program not behaving as expected, double is converting up when it shouldn't be.

Hi guys, I was making a program that rounds up numbers with various decimal places, so as an example 2001.3666 would end up as 2001.37, I managed to make this work by adding 0.005 then times 100 and converted to a int and then divided again by 100. Everything worked fine, no issues there, was having some fun making some loops too and ...

porting ostream::opfx / osfx from Unix to Linux

I am porting some C++ code from Unix to Linux (Red Hat). I have run into the following pattern: ostream& myfunction(ostream& os) { if (os.opfx()) { os << mydata; os.osfx(); } return os; } The functions opfx and osfx are not available under Red Hat 4.5. I saw a suggestion here to use the ostream::sentry functionality:...

Converting ostream into standard string

Hello all, I am very new to the C++ STL, so this may be trivial. I have a ostream variable with some text in it. ostream* pout; (*pout) << "Some Text"; Is there a way to extract the stream and store it in a string of type char*? ...

Calculating bandwidth speed of data sent/received over a socket?

Does anyone have any tips on how to calculate the bandwidth usage of a socket? For example, as I send data over a socket to the server I am connected to, I want to show the Kb/s that is being sent. Google search didn't reveal anything useful. Maybe I'm searching the wrong terms. ...

how can I read exactly 128 bytes from an fstream into a string object?

How do I read exactly 128 bytes from an fstream into a string object? I wrote some code to read the first 128 bytes of a file and print it and then the last 128 bytes of the file and print that. The last part works, since you can easily iterate to EOF, but how do I get exactly 128 bytes from the front? The code below doesn't work since ...

How to read formatted data in C++?

I have a formatted data like the following: Words 5 AnotherWord 4 SomeWord 6 It's in a text file and I'm using ifstream to read it, but how do I separate the number and the word? The word will only consist of alphabets and there will be certain spaces or tabs between the word and the number, not sure of how many. ...

iostream - reading string with embedded blanks

I have a file with records that looks like this 123 Tag Now is the time for all good men to come to the aid There always a number and some tag followed by a series of words. I want to extract the number as integer, tag as string, and sentence as string. I've done this using getline and scan plus some substring foolishness. Is there ...

creating input iostream input manipulator

As an exercise, I'm trying to create a input stream manipulator that will suck up characters and put them in a string until it encounters a specific character or until it reaches eof. The idea came from Bruce Eckel's 'Thinking in c++' page 249. Here's the code I have so far: #include <string> #include <iostream> #include <istream> #in...

understanding Java NIO concepts

Can someone suggest a good book/tutorial to understand Java NIO from the operating system perspective (in comparison with java.io?) ...

Why does istream_iterator<unsigned char, unsigned char> throw std::bad_cast?

What is going on? #include <iostream> #include <iterator> #include <sstream> int main() { std::basic_stringbuf<unsigned char> buf; std::basic_istream<unsigned char> stream(&buf); // the next line throws std::bad_cast on g++ 4.4 std::istream_iterator<unsigned char, unsigned char> it(stream); } I've tried stream.write(s...

How reliable are basic_stringbuf::in_avail() and basic_stringbuf::str()?

Hi, I need to get all the contents from a stream, without actually extracting them (just like stringstream::str()). I've tried basic_stringbuf::str(), but it behaves incorrectly when the stream is empty. To avoid that case, I had a go at basic_stringbuf::in_avail(), but that hasn't worked out very well either. In the following test ca...

How to create a boost ssl iostream?

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 iostre...

Pipes to C++ Streams

is it possible to turn pipes genereated via pipe() on a POSIX-system into std::istreams and std::ostreams? if yes, how? i would prefer to use << and >> instead of read() and write() thanks in advance ...

WriteLog( ... ) function in C++

I'm trying to find a decent way to do logging from C++. My current solution is this: ostream & GetLog() { if( output == NULL ) throw error; return *output; } Where output is defined somewhere and can be a file or whatever. This is fine, but it doesn't let me do anything other than throw an error if output is not allocated. Also, my pr...

Getting result of executed shell command fails/hangs

I try to execute shell commands, this does work as it should. Even the result comes back (as is see on LogCat). The problem ist the last line of the result. Every time a readLine() on the last line occurs (which shouldn't occur, temp should be null), the app hangs forever and doesn't come back from the readLine call. Maybe you find the ...

Program is skipping over Getline() without taking user input

This is a very strange problem, when my program asks the user for the address, instead of waiting for input, it seems to skip the getline() function completely Answerinput: cout << "would you like to add another entry to the archive? (Y/N):"; cin >> answer; cout << endl; cout << endl; answer = toupper(answer); switch(answer) ...

How are iostream objects cin, cout, cerr, and clog implemented?

iostream objects cin, cout, cerr, and clog are objects declared in the iostream header. I'm aware that it's possible in some compilers to attempt to use these iostream objects before they are constructed, so under some circumstances they must be subject to the "static initialisation order fiasco". In those compilers where it's always s...

Same C++ code results in infinite loop on Windows and expected behavior on OSX

This is one of the weirder things I've seen. I'm teaching an intro C++ course at a university, and one of my students contacted me saying that his code was running forever without stopping. I briefly glanced over his code during class, and didn't see anything immediately obvious, so I had him email me his code. Without making any chang...

Injecting string to 'cin'

I have a function that reads user input from std::cin, and I want to write a unittest that inserts some strings into std::cin, such that later extraction from std::cin will read that string instead of pausing for keyboard input. Ideally, I would change the function signature so that I can pass a custom istream as parameters, but I can't...