0

I am trying to write in binary format on console in FORTRAN under windows but I can not do it. I know I can use Open() function and use Form='Unformatted' to open a file and write in binary format. But I need to write on the console not external file.

My project is to use pipe mechanism to communicate between a FORTRAN and a C++ programs. Part of the communication is to send large amount of double precision numbers. Right now I am sending the actual numbers. If I can send them as binary format it will speed up the process.

Is there any way to write in binary format on console in FORTRAN? I tried to typecast the numbers by char() function but it is only for integer not double!

Any help will be appreciated

Thanks

3
  • The trick is to stuff the data into strings and use ordinary list directed write.. stackoverflow.com/questions/13027067/…
    – agentp
    Mar 22, 2013 at 19:23
  • Thanks for the quick reply george but the page you mentioned is for Linux not windows! do you know how to do it under windows?
    – VecTor
    Mar 22, 2013 at 21:12
  • it should be the same, try it and see what problems you run into.
    – agentp
    Mar 23, 2013 at 13:24

1 Answer 1

0

If it is F77, try something like this

      program writer
      double precision x
      character(len=8) xalt
      equivalence(x,xalt)
      x = 10.0
      do i = 1, 10, 1
         x = -10.0 * x
         write(*,'(A)', advance='no') xalt
      end do
      stop
      end

If you want to use it for F90 without the equivalence, you will have to use the transfer function to transfer from double precision (or real*8 or real*2 depending on implementation) to character.

I don't really know how big your datastream is but if you are writing to console, the OS might insist on inserting a CR/LF after X characters. That could completely mess up your input stream. Ever thought of doing this using MPI or sockets or pipes?

3
  • advance='no' is a little advanced for F77. The fortran runtime might do interesting things to the data stream as well if part of the representation of the double precision value looks like a line feed.
    – IanH
    Mar 22, 2013 at 22:51
  • old school you do write(*,'(a,$)'). The $ is not strictly standard f77 but a common extension.
    – agentp
    Mar 23, 2013 at 13:17
  • for completeness i have found you cannot READ stdin with this approach, exactly due to the issue of spurious data bits being interpreted as an EOF.
    – agentp
    Mar 23, 2013 at 13:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.