[英]Fortran read binary file from C#
I am trying to read a binary in fortran. 我正在尝试阅读fortran中的二进制文件。 The file is created in c#.
该文件在c#中创建。 In my example below it just stores to integer (4 byte I assume since the created file size is 8 byte):
在我的下面的例子中它只存储到整数(我假设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);
}
The fortran code to read the data : 用于读取数据的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
The open
and the inquire
commands runs wihtout problem. open
和inquire
命令运行没有问题。 In debug-mode it reports that after open
ios
is 0 and after inquire
acc = 'STREAM', ex = .true., frm='UNFORMATTED', irec = -1, nam is the full path to the file, nr = 0, op = .true., p = 1 and seq = 'YES'. 在调试模式下,它报告在
open
ios
为0并且在inquire
acc ='STREAM'之后,ex = .true。,frm ='UNFORMATTED',irec = -1,nam是文件的完整路径,nr = 0 ,op = .true。,p = 1和seq ='YES'。
when I run the first read statement ios = -1 indicating end-of-file and numMeas is not set to anything, same goes for read statement 2. 当我运行第一个读语句ios = -1表示文件结束而numMeas没有设置为任何东西时,读语句2也是如此。
Does anyone knows what is wrong here? 谁有人知道这里有什么问题? Every hint is much appreciated.
每个提示都非常感激。 I have tried to search for hints but haven't succeded in finding out what's going wrong...
我试图寻找提示,但没有成功找出出了什么问题......
so stupid of me... During debugging I had first tried without all the verifications, iostats, inquire etc.. I had also not included the status='OLD'
in the open statement, whereby a 0 sized file had been created in the ../../
(base project folder) and not where I thought it should have been (in the /TestFortran/Debug/
directory ). 我这么傻......在调试过程中,我第一次尝试没有所有的验证,iostats,查询等等。我还没有在open语句中包含
status='OLD'
,因此在那里创建了一个0大小的文件../../
(基础项目文件夹)而不是我认为应该在的地方(在/TestFortran/Debug/
目录中)。 Sorry guys for using your time in vain. 对不起,你们白白浪费时间。 But thanks for your quick responses.
但感谢您的快速回复。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.