簡體   English   中英

Fortran從C#讀取二進制文件

[英]Fortran read binary file from C#

我正在嘗試閱讀fortran中的二進制文件。 該文件在c#中創建。 在我的下面的例子中它只存儲到整數(我假設4字節,因為創建的文件大小是8字節):

string binfile = @"c:\..\bindata.dat";
int nrecs = 60*60*24*61;
int nvalues = 34;

using (BinaryWriter bw = new BinaryWriter(File.Open(binfile, FileMode.Create)))
      {
        bw.Write(nrecs);
        bw.Write(nValues);        
      }

用於讀取數據的fortran代碼:

program TestFortran

    implicit none

    ! Variables
    character(*),parameter      ::  fn = "bindata.dat"
    integer                     ::  numMeas,numVals
    integer                     ::  infile,ios
    character(len=11)           ::   acc, seq, frm
    character(len=128)          ::  nam
    logical                     ::  ex,op
    integer                     :: irec, nr,p
    ! Body of TestFortran
    print *, 'Hello World'
    infile = 10
    open(unit = infile,file = fn,status='OLD',access='STREAM',FORM='UNFORMATTED',action='READ',iostat=ios)
    inquire(infile,err=99,exist=ex,opened=op,name=nam,access=acc,&
      sequential= seq, form = frm, recl=irec, nextrec=nr,pos=p)
    read(infile,IOSTAT=ios)  numMeas
    read(infile,IOSTAT=ios)  numVals
99  close(infile)
    write(*,'(a,i8,a,i6)')  "Number of measurements = ", numMeas, ", and number of values in a measurement = ", numVals

end program TestFortran

openinquire命令運行沒有問題。 在調試模式下,它報告在open ios為0並且在inquire acc ='STREAM'之后,ex = .true。,frm ='UNFORMATTED',irec = -1,nam是文件的完整路徑,nr = 0 ,op = .true。,p = 1和seq ='YES'。

當我運行第一個讀語句ios = -1表示文件結束而numMeas沒有設置為任何東西時,讀語句2也是如此。

誰有人知道這里有什么問題? 每個提示都非常感激。 我試圖尋找提示,但沒有成功找出出了什么問題......

我這么傻......在調試過程中,我第一次嘗試沒有所有的驗證,iostats,查詢等等。我還沒有在open語句中包含status='OLD' ,因此在那里創建了一個0大小的文件../../ (基礎項目文件夾)而不是我認為應該在的地方(在/TestFortran/Debug/目錄中)。 對不起,你們白白浪費時間。 但感謝您的快速回復。

暫無
暫無

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

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