Formatting fread binary file (converting from FORTRAN script)

I am converting some Fortran to MATLAB and am stuck early on when trying to load a binary file. I know MATLAB well but I have not had much experience loading binary files. I have mostly had to only load text files in the past. Furthermore, I am a newbie to Fortran and the I/O commands and generally lower-level language is a challenge.
The Fortran script has a read statement:
READ(Ifile,FMT='(A70)',ERR=993) Tag
My understanding is that this command has a file id (Ifile) similar to "fid" which has been called using a previous OPEN command (similar to fopen).
Furthermore, the '(A70)' specifies that the read command should read 70 bytes of the file as a character (A).
The result is stored in the variable "Tag".
However, in MATLAB, if I use
Tag = fread(fid,70,'char')
I don't get the same result as Fortran.
What am I missing here? Any help is appreciated.

3 件のコメント

dpb
dpb 2020 年 7 月 16 日
編集済み: dpb 2020 年 7 月 16 日
The Fortran variable Ifile is an IO UNIT number returned by a preceding OPEN statement, yes. It is the same thing as a file handle functionally albeit Fortran uses simple integers as the representation at the user level. That's of no concern here, just background information.
BUT, that would be a formatted text file...the "FMT=" keyword would not be present for an UNFORMATTED file.
What about Endian'ness of the file on the Fortran system vis a vis the MATLAB platform, etc., etc., etc., ... ?
Info on the machine, OS, etc., that wrote the file and ditto for what trying to read on could be significant.
We dunno any of those particulars...
Attach the Fortran input file...
Specifically, it would let us know precisely what to expect if you showed the OPEN statement and the variable declaration for "Tag"
James Tursa
James Tursa 2020 年 7 月 16 日
編集済み: James Tursa 2020 年 7 月 16 日
You might have to skip 4 bytes first, and use '*char' formatting (with the asterisk) to get a char result. E.g., you might have to use some variation of these lines:
fread(fid,[1 4],'*uint8');
Tag = fread(fid,[1 70],'*char',4);
dpb
dpb 2020 年 7 月 16 日
Specifically, the file could be SEQUENTIAL or DIRECT. If it is direct access, then there will be as James notes a record marker at beginning (and perhaps end) of each record that will have to skip; a sequential file will have only record terminators and can be variable length; direct access is fixed-length.

サインインしてコメントする。

回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeFortran with MATLAB についてさらに検索

製品

リリース

R2020a

質問済み:

2020 年 7 月 15 日

コメント済み:

dpb
2020 年 7 月 16 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by