| Register | FAQ | Calendar | Search | Today's Posts | Mark Forums Read |
|
#1
| |||
| |||
| Dear all, is that possible to read some binary files written by Fortran language in a different language such as C++? I read here: http://www.math.utah.edu/software/c-with-fortran.html that Fortran uses "records" which makes things hard for other programming languages to process the bytes in some form of these records. However since the document is from 2001 and I would like to turn to the group for an advice or direction. Just curious... Thx for the replies. |
|
#2
| |||
| |||
| utab wrote: > Dear all, > > is that possible to read some binary files written by Fortran language in > a different language such as C++? > > I read here: > > http://www.math.utah.edu/software/c-with-fortran.html > > that Fortran uses "records" which makes things hard for other programming > languages to process the bytes in some form of these records. However > since the document is from 2001 and I would like to turn to the group for > an advice or direction. Just curious... "Possible", yes. A quick perusal of the contents of the link indicates (to me) it is written w/ the the undergrad new programming student in mind and so makes things that aren't necessarily so absolutes. The new Standard does formalize the concept of "stream" binary i/o and virtually any compiler one is likely to find today has a technique to do so if it doesn't yet comply w/ the new Standard so the situation has improved some. "Unformatted" i/o is, otoh, still compiler dependent and while possible, not particularly amenable to the use of transporting between languages--that part is still and will continue to be true. If there's more depth required/needed, ask specifics... -- |
|
#3
| |||
| |||
| On Sep 3, 7:13*pm, utab <umut.ta...@gmail.com> wrote: > Dear all, > > is that possible to read some binary files written by Fortran language in > a different language such as C++? > > I read here: > > http://www.math.utah.edu/software/c-with-fortran.html > > that Fortran uses "records" which makes things hard for other programming > languages to process the bytes in some form of these records. However > since the document is from 2001 and I would like to turn to the group for > an advice or direction. Just curious... > > Thx for the replies. If you know the file and record structure, yes. Once you can read an arbitrary number of bytes in as raw data, the rest is a programming exercise. [Try it in Pascal with blockread, just for fun!] - e |
|
#4
| |||
| |||
| On Wed, 03 Sep 2008 18:19:49 -0500, dpb wrote: > utab wrote: >> Dear all, >> >> is that possible to read some binary files written by Fortran language >> in a different language such as C++? >> >> I read here: >> >> http://www.math.utah.edu/software/c-with-fortran.html >> >> that Fortran uses "records" which makes things hard for other >> programming languages to process the bytes in some form of these >> records. However since the document is from 2001 and I would like to >> turn to the group for an advice or direction. Just curious... > > "Possible", yes. A quick perusal of the contents of the link indicates > (to me) it is written w/ the the undergrad new programming student in > mind and so makes things that aren't necessarily so absolutes. > > The new Standard does formalize the concept of "stream" binary i/o and > virtually any compiler one is likely to find today has a technique to do > so if it doesn't yet comply w/ the new Standard so the situation has > improved some. > > "Unformatted" i/o is, otoh, still compiler dependent and while possible, > not particularly amenable to the use of transporting between > languages--that part is still and will continue to be true. > > If there's more depth required/needed, ask specifics... Hmm, thanks for the reply, quite late here but let me think very easily, suppose I have this code program binary_write double precision :: a = 2.458899, b= 132230.2323 open(unit=5, file = 'test.bin', form='unformatted') write(5) a, b close(5) end program binary_write which writes two double precision values in an unformatted file and that is going to be the file I would like to read in C++.(Frankly speaking I was looking at sth quite different solely in C++ but ended up here, let me see what I can get out of this search )Thx for your time, Best |
|
#5
| |||
| |||
| utab wrote: .... > suppose I have this code > > program binary_write > > double precision :: a = 2.458899, b= 132230.2323 > open(unit=5, file = 'test.bin', form='unformatted') > write(5) a, b > close(5) > > end program binary_write > > which writes two double precision values in an unformatted file and that > is going to be the file I would like to read in C++. ... As noted, that's the area that isn't transportable automagically but can be worked out as noted. What I would suggest is to first look at the language manual for the compiler you're using and see what it's particular syntax is for "stream" or "binary" i/o. It's virtually positive it will have a way implemented and while it's not necessarily portable between compilers, it is common enough that any compiler will have the facility w/ only a minor change in the OPEN statement at worst and it will output files that are readable in C/C++ w/o dealing w/ the Fortran record markers. This, of course, assumes the same platform so issues of representation, byte order, etc., aren't a possible issue. -- |
|
#6
| |||
| |||
| On Wed, 03 Sep 2008 16:53:58 -0800, glen herrmannsfeldt wrote: > utab wrote: > >> is that possible to read some binary files written by Fortran language >> in a different language such as C++? > > On most systems the records are indicated by a four byte length field at > the beginning, and on many by another four byte length at the end of > each record. (The latter makes it easier to do BACKSPACE.) > > Assuming you know the length, fread() four bytes and ignore them, > fread() the record (in one or multiple calls), and, if needed fread() > the record end marker. > > double precision :: a = 2.458899, b= 132230.2323 open(unit=5, > file = 'test.bin', form='unformatted') write(5) a, b > close(5) > > double a,b; > char tmp[8]; > FILE *in; > in=fopen("test.bin","r"); > fread(tmp,1,4,in); > fread(&a,sizeof(a),1,in); > fread(&b,sizeof(b),1,in); > printf("%g %g\n",a,b); > fread(tmp,1,4,in); /* not always needed */ fclose(in); > > It is good practice to test the return values of fopen(), fread(), and > fclose(), unlike the program above. > > -- glen Thanks glen, this was really helpful but still should check the compiler documentation as "dpb" suggested I guess, at least I will know more then... |
|
#7
| |||
| |||
| utab wrote: > is that possible to read some binary files written > by Fortran language in a different language such as C++? On most systems the records are indicated by a four byte length field at the beginning, and on many by another four byte length at the end of each record. (The latter makes it easier to do BACKSPACE.) Assuming you know the length, fread() four bytes and ignore them, fread() the record (in one or multiple calls), and, if needed fread() the record end marker. double precision :: a = 2.458899, b= 132230.2323 open(unit=5, file = 'test.bin', form='unformatted') write(5) a, b close(5) double a,b; char tmp[8]; FILE *in; in=fopen("test.bin","r"); fread(tmp,1,4,in); fread(&a,sizeof(a),1,in); fread(&b,sizeof(b),1,in); printf("%g %g\n",a,b); fread(tmp,1,4,in); /* not always needed */ fclose(in); It is good practice to test the return values of fopen(), fread(), and fclose(), unlike the program above. -- glen |
|
#8
| |||
| |||
| On Sep 3, 5:39 pm, utab <umut.ta...@gmail.com> wrote: > program binary_write > > double precision :: a = 2.458899, b= 132230.2323 > open(unit=5, file = 'test.bin', form='unformatted') > write(5) a, b > close(5) > > end program binary_write All of the machines I work on are Unix/Linux (includes Max OS X), so what I am about to say applies to these. I don't know anything about doing Fortran in the Microsoft world. This program will, after compiling and execution, leave you with a file called test.bin in the current working directory of size 24 bytes. The first four bytes are an integer "record control word" which is nothing more than the size of the record in bytes (not including the record control words). The next 16 bytes are the two doubles you wrote. The last 4 bytes are identical to the first four bytes. (We need this because Fortran has a BACKSPACE function for sequential access files.) There is one exception to this that I have found: The g77 compiler running on some 64-bit systems uses 8-byte record control words. So the above program would produce a file of size 32 bytes. Hence, you could read through a sequential access Fortran binary (such as written by the above program) by reading the first four bytes as an integer, then reading that integer number of bytes as doubles, then reading the closing four bytes of that record. Repeat as needed to get to the record you want. Of course this might not be the most efficient way to pull out the records you need, especially if all of the records are the same length. Hope this helps more than hinders, Andy |
![]() |
| Thread Tools | |
| Display Modes | |
In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.