CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > OpenFOAM > OpenFOAM Bugs

IFstream binary file read type conversion problem

Register Blogs Community New Posts Updated Threads Search

Like Tree1Likes
  • 1 Post By card

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   October 24, 2008, 15:02
Default In OpenFOAM 1.5, I believe the
  #1
New Member
 
David Car
Join Date: Mar 2009
Location: Dayton, OH, USA
Posts: 7
Rep Power: 17
card is on a distinguished road
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::noParallel();
argList::validArgs.append("binary file");

argList args(argc, argv);

if (!args.check())
{
FatalError.exit();
}

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

int myvar;

myFile.read(reinterpret_cast<char> &myvar), sizeof(myvar));

return 0;
}
.......

Testcase:
(preferably one of the tutorials)

Platform:
Linux gcc-4.3.1 and gcc-4.3.2, KUbuntu.

Version:
OpenFOAM 1.5


All the best,

David
Pagoda likes this.
card is offline   Reply With Quote

Old   October 24, 2008, 17:09
Default FYI, the syntax in the myFile.
  #2
New Member
 
David Car
Join Date: Mar 2009
Location: Dayton, OH, USA
Posts: 7
Rep Power: 17
card is on a distinguished road
FYI, the syntax in the myFile.read statement above did not cut and paste properly. It is and should be:

myFile.read(reinterpret_cast<char*>(&myvar), sizeof(myvar));
card is offline   Reply With Quote

Old   October 29, 2008, 11:39
Default No issues... I'm really sorry
  #3
New Member
 
David Car
Join Date: Mar 2009
Location: Dayton, OH, USA
Posts: 7
Rep Power: 17
card is on a distinguished road
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.
card is offline   Reply With Quote

Old   October 30, 2008, 14:18
Default The OpenFOAM binary streams ar
  #4
Senior Member
 
Mattijs Janssens
Join Date: Mar 2009
Posts: 1,419
Rep Power: 26
mattijs is on a distinguished road
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.
mattijs is offline   Reply With Quote

Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
IFStream read float point data problem liu OpenFOAM Running, Solving & CFD 0 October 24, 2008 12:14
invalid type for binary expression: double / funct Fiachra FLUENT 0 April 12, 2007 08:04
How to read the patch type diegon OpenFOAM Running, Solving & CFD 0 January 10, 2007 15:03
UDF Read external File problem guru FLUENT 1 September 15, 2004 06:24
Read Plot3D Binary File John Main CFD Forum 3 January 17, 2001 03:57


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