bug with ostream::operator<<

Hi,
I recently discovered a bug in an ostream::operator<< and don't know what my options are. In particular, the operator works fine when I call my library from c++ main. However, when I call my library from dynamic library via R, I can only output strings or char*'s or chars to the ostreams. What I can't do is output any numeric values such as long types or doubles. When I do that, I get a bug such that the stream completely freezes and cannot be written to anymore. I'm confused as to how this happens and wonder what I can do about it. For a specific case, I was able to manually print a string from the values and then pass it to the ostream. However, I would like to either fix the bug if I'm simply pointing to the wrong headers or libraries when compiling or override the functions manually if possible. I'm a bit confused as to how to know what I'm doing because I can't get into the standard c++ library and have it write out what it's doing. Does any one have some suggestions for me?
Thanks,
Sean
Can I see your code where you are doing this? It's possible you could be causing it to set its failbit for some reason of another.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
extern long method;
extern vector<string>newArgs;
void printArgs(ostream&out)
{
    out << "-m " << (long)method;//ERROR HAPPENS HERE 
    out.flush();
    for(vector<string>::iterator it = newArgs.begin();it != newArgs.end();it++)
    {
        if(it == newArgs.begin())
        {
            it++;
        }
        if(it != newArgs.end())
        {
            string argString = *it; 
            out << " " << argString.c_str();
        }
    }
    out << endl;
}

The error can be avoided by adding
1
2
3
4
5
6
7
8
string getStringFromLong(long i)
{
    char*intValue = (char*)calloc(1000,sizeof(char));
    sprintf(intValue,"%ld",i);
    string returnValue(intValue);
    free(intValue);
    return(returnValue);
}

and substituting
1
2
3
//out << "-m " << (long)method;//ERROR HAPPENS HERE 
//with:
out << "-m " << getStringFromLong(method);//NO ERROR THIS TIME 
Topic archived. No new replies allowed.