簡體   English   中英

Fortran:在兩個不同的程序之間交換數據

[英]Fortran: exchange data between two different program

我需要在兩個不同的fortran程序之間交換數據,不幸的是,然后需要分開(兩個不同的.exe),一個寫在f90中,另一個寫在f77中。
目前,這是我的解決方案:
-f90(.exe)在f77(master)的開頭啟動,並使用do while('true')等待來自f77的數據並查詢-將數據寫入f90的某個文件(file_in)讀取,詳細說明結果並將結果寫入新文件(file_out)-f77使用do while('true')並查詢-來等待f90的數據,然后繼續-這是f77中程序的示例代碼:

   C Write data for f90
   open(210, file=file_in, action='write', status='replace', form='unformatted')
   write(210) data
   close(210)
   do while (.true.)
       inquire(file=KDTree_out, exist=file_exist)
       if (file_exist) then
           call sleepqq(2)
           exit
       endif
   enddo
   open(220, file=KDTree_out, action='read', status='old', form='unformatted')
   read(220) value
   close(220, status='delete')

這是f90中程序的示例代碼:

do while(.true.)
    inquire(file=KDTree_in, exist=file_exist)
    if (file_exist) then
          call sleepqq(2)
          ! Read data
          open(100, file=file_in, action='read', status='old', form='unformatted')
          read(100) data
          close(100, status='delete')

          ! Elaborate data

          ! Write data     
          open(150, file=file_out, action='write', status='replace', form='unformatted')
          write(150) value
          close(150)

      endif
enddo

該解決方案似乎效果很好,但是您可以看到有一些sleepqq(2)(微秒)可以避免文件結束錯誤,但是如果您使用ssd或硬盤,則此等待時間可能會有所不同,因此不是完美的解決方案。 你有什么想法?

謝謝

多虧了agentp,這解決了我的問題:

考慮在關閉/刷新數據文件后使用第二個文件作為寫入的標志,這應該有助於解決一些時序問題

[編輯]我在使用文件來標記數據文件已准備就緒時遇到了一些問題。 我並非總是能夠刪除此文件。

這是我的解決方案,在准備好數據文件后,我在一個程序上編寫:

open(202, file=chk_file2)
close(202)

在另一個程序中,我有以下代碼來獲取標志並刪除文件,因為我需要多次交換數據:

do while (.true.)
   inquire(file=chk_file2, exist=file_exist)
   if (file_exist) then
102   continue
      istat = 1
      do while (istat.eq.0)
         open(401, file=chk_file2, iostat=istat, err=102)
         close(401, status='delete', err=102)
      enddo
      exit
   endif
enddo

要么:

do while (.true.)
   inquire(file=chk_file2, exist=file_exist)
   if (file_exist) then
102   continue
      do while (.true.)
          open(400, file=chk_file2, iostat=istat, err=102)
          if (istat.eq.0) then
              close(400, status='delete', err=102)
              exit
          endif
      enddo
      exit
   endif
enddo

謝謝

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM