开发者

How to create a direct access, unformatted binary file with multiple records from .csv data?

Does anyone have any pointers for how I can create an unformatted, direct access binary file with multiple records from .csv data using FORTRAN (or any other language that lets you write binary files....)? Hopefully the question is simple enou开发者_StackOverflow中文版gh to not require sample code. Doesn't have to be .csv, either. All I need is a general method for converting a delimited text document into a direct access, unformatted binary file with multiple records.

Cheers, Aaron


You can just read it with formatted IO into an array and write it with unformatted direct access again.

Like this:

program test
  implicit none

  integer :: dat(3)
  integer :: rl

  open(file="data.csv", unit=23, action="read", status="old")

  read(23,*) dat

  close(23)

  inquire(iolength=rl) dat
  open(file="data.bin", unit=24, action="write", status="replace", &
    &  form="unformatted", access="direct", recl=rl)
  write(24,rec=1) dat
  close(24)

end program test

This would read 3 values from the file data.csv, which might look like this: 1, 2, 3 and write it in binary to the file data.bin.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜