有没有一种简单的方法可以在 FORTRAN 中读取文件并打印到标准输出?

发布于 2024-11-02 13:55:47 字数 73 浏览 0 评论 0原文

我有一个数据文件想要打印到标准输出。这是否可以在 Fortran 中完成,而无需将数据读入数组,然后打印数组?

谢谢

I have a data file that I want to print to stdout. Is this possible to do in fortran without having to read the data into arrays and then printing the arrays?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

满地尘埃落定 2024-11-09 13:55:47

您可以打开文件进行流访问,并在循环中一次处理一个字符,直到到达文件末尾。我不知道这对于大型数据文件来说有多高效,但它使您不必定义一个长度足以容纳最长行的字符变量,这需要有根据的猜测。

program echostd
    use, intrinsic :: iso_fortran_env, only: iostat_end
    implicit none

    character(*), parameter :: file_name = 'data.txt'

    integer   :: lun, io_status
    character :: char

    open(newunit=lun, file=file_name, access='stream', status='old',  &
         action='read')

    print *, '--- Content of file: ' // file_name // ' ---'

    do
        read(lun, iostat=io_status) char
        if(io_status == iostat_end) exit
        if(io_status > 0) error stop '*** Error occurred while reading file. ***'
        write(*, '(a)', advance='no') char
    end do

    print *, '--- End of content of file: ' // file_name // ' ---'

    close(lun)
end program echostd

我已经使用了两个 Fortran 2008 功能,但如果您的编译器尚不支持它们,那么没有它们也可以轻松完成。
一是 newunit= 说明符;如果您的编译器不支持这一点,您当然可以使用预定义的单元号。
另一种是error stop语句;如果需要,只需删除 error 部分即可。

You can open the file for stream access and process it one character at a time in a loop, until end of file is reached. I have no idea how (in)efficient this may be for large data files, but it saves you from having to define a character variable with a length large enough to hold the longest line, which requires an educated guess.

program echostd
    use, intrinsic :: iso_fortran_env, only: iostat_end
    implicit none

    character(*), parameter :: file_name = 'data.txt'

    integer   :: lun, io_status
    character :: char

    open(newunit=lun, file=file_name, access='stream', status='old',  &
         action='read')

    print *, '--- Content of file: ' // file_name // ' ---'

    do
        read(lun, iostat=io_status) char
        if(io_status == iostat_end) exit
        if(io_status > 0) error stop '*** Error occurred while reading file. ***'
        write(*, '(a)', advance='no') char
    end do

    print *, '--- End of content of file: ' // file_name // ' ---'

    close(lun)
end program echostd

I've used two fortran 2008 features, but they can both easily be done without, if your compiler doesn't support them yet.
One is the newunit= specifier; if your compiler doesn't support this, you can of course use a predefined unit number.
The other is the error stop statement; simply remove the error part if needed.

甚是思念 2024-11-09 13:55:47

好的,我只是有点好奇你在哪里使用 Fortran。

您可以将其读入循环内的字符变量中。 Fortran 有一个声明,比如

read (7,*,end=10) name

您可以继续阅读直到到达 EOF(并且在 EOF 时它将跳转到第 10 行)。

OK, I'm just mildly curious where you're using Fortran.

You can just read it into a character variable inside a loop. Fortran has a statement like

read (7,*,end=10) name

where you can continue reading until you reach and EOF (and it will jump to line 10 on EOF).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文