views:

498

answers:

3

I'm trying to write binary data files from fortran, but I find the regular file interfaces very limiting, I wonder if Fortran has POSIX compilant functions.

I found this standard: IEEE 1003.9-1992, but I don't know it if is fully supported by most common compilers (or if I have to activate any flags). I can't find any practical information, can you give me any suggestion?

BTW I'm trying to write image files, first I want to try with tga/pgm. Also I don't want to add any dependency to external libraries, thanks!

Solution:

Finally I was able to write ppm files, and I had to avoid printing newline at the end of the internal loop, the final code is like this:

      subroutine imgwrite()
      implicit none
*     ******************************************************************
      include 'image.f'
*     ******************************************************************
      integer x, y, i

      write(imgunit, '(A)') 'P3'
      write(imgunit, '(I4)') imgwidth
      write(imgunit, '(I4)') imgheight
      write(imgunit, *) 255

      do 10 y=1, imgheight
        do 20 x=1,imgwidth
          write(imgunit,100) (int(imgpixels(x,y,i)*255D0), i=1,3)
20      continue
        write(imgunit,110)
10    continue


100   FORMAT(3(I4),$)
110   FORMAT((/))

      return
      end

Nobody provided any information about POSIX in FORTRAN.

Thanks.

+4  A: 

TGA is a very plain format even though it is binary, so it should be possible to write it from FORTRAN. I wouldn't want to attempt TIFF, PNG, GIF, or JPG in FORTRAN without the help of external libraries, however.

You might find that one of the family of netpbm formats is easier to write, and they would be easily converted to TGA or any other format you might need for display or further processing with simple commands. The PBM formats include both pure ASCII and binary flavors so they can be written (albeit very inefficiently) even if binary file access is problematic. The tool suite includes conversion to and from about 100 other image file formats.

Edit: Sorry that I can't be more helpful about how to actually go about writing files from FORTRAN, but punch cards were common when I last used the language. ;-)

RBerteig
A: 

I don't know if this is useful. I haven't checked it

http://sourceforge.net/projects/fortranposix/

In any case, I have a question. Are you opening the file in sequential or random access mode ?

Stefano Borini
A: 

Problem with reading and writing binary files, random or sequential, for other languages to use is that the fortran io system uses other information in the file. I cannot recall it being in the f77 standard but certainly most pc and unix compilers did this.

For example if writing to a binary sequential file

write(bsf) doublevariable, realvariable

then the file would contain (if you could read binary)

12<value of double><value of real>12

Each record has record markers which are the length in bytes of what was written at the front and end. This is needed because each record can have a different length and when reading one does not have to read everything that was written for a record. For example if one was reading the above one could have

read(bsf) doublevariable

The next read of bsf needs to be at the start of the next record.

So reading or writing binary files to share with other languages should be done via a library written in C or any language that does not do what Fortran does,

hugok