views:

47

answers:

1

Good afternoon,

I'm attempting to get some old-fashioned code to function in g++ 4.4. My understanding is the code compiled fine in g++ 4.0, but g++ has become more strict between 4.0 and 4.4. This code in particular causes the compiler to stop and complain:

sprintf(s,"%.7d",(long)tellp());

tellp() is a std::streampos object. The above call to sprintf occurs inside a derived class from a std::fstream class.

The code intends to cast data that indicates the position in an fstream object to a numerical type, so that it can be passed to a 2nd running program (via a file) so that the 2nd program can quickly look-up some data in a file.

Is there a simple way to rescue this, with a cast g++ would be happy with?

The error message:

error: format ‘%.7d’ expects type ‘int’, but argument 3 has type ‘std::streamoff’
+1  A: 

There doesn't appear to be a portable way to do this, since std::fpos is basically implementation defined. That said, gcc doesn't treat a std::streampos as much of anything like a number. On the other hand, you can convert it into a std::streamoff, which is architecture dependent for its actual type, but is some manner of typedef to long or long long or whatever. Subtract the desired offset from the first position of the stream and it returns std::streamoff which can be trivially converted to long.

TokenMacGuy