Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Binary files with gfortran-4.8 1

Status
Not open for further replies.

stamcose

Programmer
Apr 21, 2018
3
0
0
DE
Using "gfortran-4.8" I want to create and subsequently use a binary file! According to the information I have seen the format specifications "UNFORMATED" and "BINARY" should both be available, the exact difference between these alternatives not being clear to me. But both these alternatives are unknown to the compiler:

mats@linux-ze70:~/WDB> c try
try.f:1.72:

OPEN(9,FILE='shoreline',ACCESS='SEQUENTIAL',FORM='UNFORMATED')
1
Error: FORM specifier in OPEN statement at (1) has invalid value 'UNFORMATED'
mats@linux-ze70:~/WDB> c try
try.f:1.72:

OPEN(9,FILE='shoreline',ACCESS='SEQUENTIAL',FORM='BINARY')
1
Error: FORM specifier in OPEN statement at (1) has invalid value 'BINARY'

Sure, because of large storage devices the binary files are less often used as before! But this option should still be available!

How to proceed?
 
And leaving out the option is no solution! Then a "FORMAT" is indeed required!

mats@linux-ze70:~/WDB> cat try.f
F=3.14
OPEN(9,FILE='shoreline',ACCESS='SEQUENTIAL')
WRITE(9) F
STOP
END
mats@linux-ze70:~/WDB> c try
mats@linux-ze70:~/WDB> rm shoreline
mats@linux-ze70:~/WDB> r try
+ test 1 -gt 1
+ fil=try
+ test -f try.i
+ ./try.bin
At line 3 of file try.f (unit = 9, file = 'shoreline')
Fortran runtime error: Missing format for FORMATTED data transfer
 
Hi stamcose,

I have this version of gfortran
Code:
$ gfortran --version
GNU Fortran (Ubuntu 4.8.4-2ubuntu1~14.04.4) 4.8.4
...
and form='unformatted' works for me. It creates a binary file.

Here is the example I tried
stamcose.f95
Code:
[COLOR=#800080]program[/color] stamcose
  [COLOR=#2e8b57][b]implicit[/b][/color] [COLOR=#2e8b57][b]none[/b][/color]
[COLOR=#2e8b57][b]  real[/b][/color] :: a, b

  a [COLOR=#a52a2a][b]=[/b][/color] [COLOR=#ff00ff]3.14[/color]
  b [COLOR=#a52a2a][b]=[/b][/color] [COLOR=#ff00ff]0[/color]
  [COLOR=#0000ff]! open file fro writing[/color]
  [COLOR=#a52a2a][b]open[/b][/color] ([COLOR=#ff00ff]9[/color], [COLOR=#a52a2a][b]file[/b][/color][COLOR=#a52a2a][b]=[/b][/color][COLOR=#ff00ff]'stamcose.dat'[/color], [COLOR=#a52a2a][b]form[/b][/color][COLOR=#a52a2a][b]=[/b][/color][COLOR=#ff00ff]'unformatted'[/color], [COLOR=#6a5acd]&[/color]
       [COLOR=#a52a2a][b]access[/b][/color][COLOR=#a52a2a][b]=[/b][/color][COLOR=#ff00ff]'sequential'[/color], [COLOR=#a52a2a][b]status[/b][/color][COLOR=#a52a2a][b]=[/b][/color][COLOR=#ff00ff]'replace'[/color])
  [COLOR=#0000ff]! write data[/color]
  [COLOR=#a52a2a][b]write[/b][/color]([COLOR=#ff00ff]9[/color]) a
  [COLOR=#a52a2a][b]write[/b][/color]([COLOR=#a52a2a][b]*[/b][/color],[COLOR=#a52a2a][b]*[/b][/color]) [COLOR=#ff00ff]'data written to the file: a ='[/color], a   
  [COLOR=#0000ff]! close file[/color]
  [COLOR=#a52a2a][b]close[/b][/color]([COLOR=#ff00ff]9[/color])

  [COLOR=#0000ff]! open file for reading[/color]
  [COLOR=#a52a2a][b]open[/b][/color] ([COLOR=#ff00ff]9[/color], [COLOR=#a52a2a][b]file[/b][/color][COLOR=#a52a2a][b]=[/b][/color][COLOR=#ff00ff]'stamcose.dat'[/color], [COLOR=#a52a2a][b]form[/b][/color][COLOR=#a52a2a][b]=[/b][/color][COLOR=#ff00ff]'unformatted'[/color], [COLOR=#6a5acd]&[/color]
       [COLOR=#a52a2a][b]access[/b][/color][COLOR=#a52a2a][b]=[/b][/color][COLOR=#ff00ff]'sequential'[/color], [COLOR=#a52a2a][b]status[/b][/color][COLOR=#a52a2a][b]=[/b][/color][COLOR=#ff00ff]'old'[/color])
  [COLOR=#0000ff]! read data[/color]
  [COLOR=#a52a2a][b]read[/b][/color]([COLOR=#ff00ff]9[/color]) b
  [COLOR=#a52a2a][b]write[/b][/color]([COLOR=#a52a2a][b]*[/b][/color],[COLOR=#a52a2a][b]*[/b][/color]) [COLOR=#ff00ff]'data read from the file:  b ='[/color], b 
  [COLOR=#0000ff]! close file[/color]
  [COLOR=#a52a2a][b]close[/b][/color]([COLOR=#ff00ff]9[/color])
[COLOR=#800080]end program[/color]

Compiling and running it I get following output:
Code:
$ gfortran stamcose.f95 -o stamcose
$ ./stamcose
 data written to the file: a =   3.14000010    
 data read from the file:  b =   3.14000010
Using less I could look what's in the unformatted file stamcose.data
Code:
$ less stamcose.dat 
"stamcose.dat" may be a binary file.  See it anyway?
after pressing y it shows me the binary data:
Code:
^D^@^@^@<C3><F5>H@^D^@^@^@
stamcose.dat (END)
 
Man, now I see what's wrong with your code.
You only have typo in your code !
Instead of FORM='UNFORMATED' you have to use FORM='UNFORMATTED'
:)
 
You are right, it was a typo! Binary files have not been abandonned! Thanks!

 
For binary files, use

.... ACCESS='STREAM'....

It is not the same as "UNFORMATTED" files.



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top