iostream

In Java: why some Stream methods take int instead of byte or even char?

Hi Folks, Why some methods that write bytes/chars to streams takes int instead of byte/char?? Someone told me in case of int instead of char: because char in java is just 2 bytes length, which is OK with most character symbols already in use, but for certain character symbols (chines or whatever), the character is being represented in ...

Why are C++ STL iostreams not "exception friendly"?

I'm used to the Delphi VCL Framework, where TStreams throw exceptions on errors (e.g file not found, disk full). I'm porting some code to use C++ STL instead, and have been caught out by iostreams NOT throwing exceptions by default, but setting badbit/failbit flags instead. Two questions... a: Why is this - It seems an odd design decis...

reading primitives from file in C

Hi Folks, I am new to C, and want to read some data from a file. Actually, I find many reading functions, fgetc, fgets, etc.. But I don't know which one/combination is the best to read a file with the following format: 0 1500 100.50 1 200 9 2 150 10 I just need to save each row above into a struct with three data members. I...

How to mix std::stream and Delphi TStream?

I'm using C++Builder, and am trying to slowly migrate code to using C++ standard library in preference to the Delphi VCL. The VCL has a streaming architecture based around the TStream class, and I'm switching to using std::stream instead. However, in the short term, I still need a way of 'mixing' use of the two stream types. I can do ...

Why can't open4 read from stdout when the program is waiting for stdin?

I am using the open4 gem and having problems reading from the spawned processes stdout. I have a ruby program, test1.rb: print 'hi.' # 3 characters $stdin.read(1) # block And another ruby program in the same directory, test2.rb: require 'open4' pid, stdin, stdout, stderr = Open4.popen4 'ruby test1.rb' p stdout.read(2) # 2 characters...

DataContractSerializer - how can I output the xml to a string (as opposed to a file)

Hi All, I had a quick question regarding the datacontractserializer. Maybe it's more of a stream question. I found a piece of code that writes the xml to a filestream. I basically don't want the file and just need the string output. public static string DataContractSerializeObject<T>(T objectToSerialize) { var fs = new ...

how to manipulate content ostream

Hi, I have a C++ code that has a lot of functions which receives ostream as argument. I now want to string manipulate the content of this ostream. For example, I want to replace all occurrence of certain word with other word. The actual parameter to these functions is always ofstream. Is there a way to change the creation of this ofstr...

C++ STL in VS2008: std::ostringstream throws std::bad_alloc after heavy assign/clear usage

Hi all, I have come across a situation (on Win32) where the std::ostringstream object continues to consume process memory, even when it is ostensibly cleared out after a series of append-type operations. Please take a look at this C++ fragment: int main(void) { std::ostringstream cOutputLogStream; // Random long string std...

How to write a pointer to std::cerr?

Given: MY_CLASS* ptr = MY_CLASS::GetSomeInstance(); What is the correct way to output ptr to std::cerr, so I can log its value? Note I don't want to write the class, just the address. ...

Memory Stream in C++ using type declarations

Possible Duplicate: Are there binary memory streams in C++ Oops - http://stackoverflow.com/questions/1559254/are-there-binary-memory-streams-in-c ...

C++ read from istream until newline (but not whitespace)

I have a std::istream which refers to matrix data, something like: 0.0 1.0 2.0 3.0 4.0 5.0 Now, in order to assess the number of columns I would like to have some code like: std::vector<double> vec; double x; while( (...something...) && (istream >> x) ) { vec.push_back(x); } //Here vec should contain 0.0, 1.0 and 2.0 where the...

Is it possible to restrict/require certain capabilities in a Stream parameter?

I'm writing an application that creates catalogs of files. Currently the catalog information is stored in an XML file, but I'm trying to abstract the interface to a catalog to allow for other future storage mechanisms such as a single ZIP file, SQL server, or HTTP server. So rather than returning a file path the abstract Catalog class re...

The best way to monitor output of process along with its execution

I have started a process in my Java code, this process take a very long time to run and could generate some output from time to time. I need to react to every output when they are generated, what is the best way to do this? ...

How do I close a socket or an I/O Stream in the windowClosing event of a Java window?

I'm having trouble closing my socket when exiting my Java application. I thought that an easy way to make sure the socket gets closed is to hook it on windowClosing in a Swing JFrame like this: frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { input.close(); out...

checking data availability before calling std::getline

Hi All I would like to read some data from a stream I have using std::getline. Below a sample using the std::cin. std::string line; std::getline( std::cin, line ); This is a blocking function i.e. if there is no data or line to read it blocks execution. Do you know if exists a function for checking data availability before calling s...

How do I handle a single text io stream with multiple inputs and outputs?

So I'm working through a bit of a problem, and some advice would be nice. First a little background, please excuse the length. I am working on a management system that queries network devices via the TL1 protocol. For those unfamiliar with the protocol, the short answer is that is is a "human readable" language that communicates via a ...

What is the << operator doing in C++?

In the example below, what exactally is the << operator doing? I'm guessing it is not a bitwise operator. std::cout << "Mouse down @ " << event.getPos() << std::endl; I understand what the code will do here: Use standard out, send this text, send an end of line. Just I've never come accross the use of this << apart from on raw binary....

io operation using same stream object.

Hello all, I want to perform I/O operation in c++. I want to store a pointer to fstream object and using that same fstream I want to read and write to that file. Is it possible without using two different objects i.e ifstream for reading and ofstream for writing. ...

Getting a byte value using stringstream

I've got this (incorrect) sample code for getting a value out of stringstream and storing it in a byte-sized variable (it needs to be in a single byte var, not an int): #include <iostream> #include <sstream> using namespace std; int main(int argc, char** argv) { stringstream ss( "1" ); unsigned char c; ss >> c; cout ...

How do I process captured output from `tail` without reprocessing?

Hi, I want to excecute a tail command in Unix for indefinite time and capture its output in a Perl script, process it and store certain data into a database. But it should be live, meaning old data – once stored in the database – shouldn't be reprocessed. It should only capture, and process only the most recent output. Can someone pleas...