Skip to content
Prev 40195 / 63421 Next

cerr and cout not working calling c++ from R

Just a short follow up to the list.
It turns out that the strange behavior I was experiencing had nothing to do
with cout and cerr, but ostreams in general. For some reason, at a minimum,
the operators: 
ostream& operator<< (long& val );
ostream& operator<< (double& val );
were not functioning properly and were causing my problem.
To get around this, I simply had to convert types like these to strings
before passing them into the ostreams with << operators.
I don't know if this was due to a mistake in which libraries I linked to or
which headers are being read when I make my project. However, it seems that
I have ruled out the possibility of this being unique to 64-bit longs as the
doubles are standard types on any machine. Anyway, I appreciate everyone's
help, and if anyone has trouble with this type of thing in the future, a
simple couple of functions I used to convert the long or double values to
strings were:
string getStringFromDouble(double d)
{
    char*doubleValue = (char*)calloc(1000,sizeof(char));
    sprintf(doubleValue,"%f",d);
    string returnValue(doubleValue);
    free(doubleValue);
    return(returnValue);
}
string getStringFromLong(long i)
{
    char*intValue = (char*)calloc(1000,sizeof(char));
    sprintf(intValue,"%ld",i);
    string returnValue(intValue);
    free(intValue);
    return(returnValue);
}
I'm not sure why this was necessary, but it solves my issue.
Thanks again for all the help--I learned a lot about streams and stream
buffers in the process.
Thanks,
Sean
On 5/8/11 12:56 AM, "Sean Robert McGuffee" <sean.mcguffee at gmail.com> wrote: