CFD Online Discussion Forums

CFD Online Discussion Forums (
-   OpenFOAM Bugs (
-   -   IFstream binary file read type conversion problem (

card October 24, 2008 15:02

In OpenFOAM 1.5, I believe the
In OpenFOAM 1.5, I believe there might be an issue with the IFstream when trying to read a binary block. See my sample code below. When tracking this down, I realized that there is also a "word" constructor with takes the arguments passed to the binary reader (IFstream::read(char *, std::streamsize) of the IFstream, i.e. a character pointer and an int. Since the "word" constructor is not declared explicit, the compiler (tried both gcc-4.3.1 and gcc-4.3.2) is doing the nice thing and type converting my arguments to a "word" before passing looking up which overloaded read function of IFstream to use. I deduced this from the following error when running the code on a simple binary file:

word too long
maximum number of characters allowed = 1024

file: flat-plate-medium-binary.g.r8 at line 1.

From function ISstream::read(word&)
in file db/IOstreams/Sstreams/ISread.C at line 81.

FOAM exiting

So it appears that its type converting by calling the word constructor and then call the appropriate read function of the IFstream. I find it hard to believe that no-one has experienced this so I feel pretty certain that I'm missing something here. Otherwise, should the word constructor be declared explicit to prevent this type conversion? Or will that introduce other issues in OpenFOAM. Many thanks.

Sample code:

#include "argList.H"
#include "IFstream.H"

using namespace Foam;

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:

int main(int argc, char *argv[])
argList::validArgs.append("binary file");

argList args(argc, argv);

if (!args.check())

IFstream::streamFormat ifmt = IFstream::BINARY;
IFstream myFile(args.additionalArgs()[0], ifmt);

int myvar;<char> &myvar), sizeof(myvar));

return 0;

(preferably one of the tutorials)

Linux gcc-4.3.1 and gcc-4.3.2, KUbuntu.

OpenFOAM 1.5

All the best,


card October 24, 2008 17:09

FYI, the syntax in the myFile.
FYI, the syntax in the statement above did not cut and paste properly. It is and should be:<char*>(&myvar), sizeof(myvar));

card October 29, 2008 12:39

No issues... I'm really sorry
No issues... I'm really sorry for the confusion... it didn't make sense with the type conversion at first... after digging for a few hours through the code base, I now see how the IFstream is implemented and realize that the binary block read is not a raw binary read, but looks for a BEGIN_LIST token... after creating a binary file with the "(" token to begin and a ")" token to end it works just fine. Again, my apologies.

mattijs October 30, 2008 15:18

The OpenFOAM binary streams ar
The OpenFOAM binary streams are specifically created to input/output OpenFOAM meshes and fields. Have a look at e.g. constant/polyMesh/owner after setting writeFormat to binary and running blockMesh.

All times are GMT -4. The time now is 08:31.