使用 C++用于读取 Fortran 记录格式文件的流

发布于 2024-12-03 20:31:31 字数 1167 浏览 0 评论 0原文

我正在寻找一种在读取 fortran 格式文件的同时在 C++ 中镜像 fortran read(*,*) 功能的方法。我熟悉并希望使用类似流的语法,但我不确定如何编写自己的/利用流来完成我需要的操作。我的文件包含以下行:

'Version' '5.3'
'This set has 1 member'
'Variable','nlo','EvolCode'
'list',1,1
1.E-05,1.,1.25,10000000.
0.00949 0.00208 -8.77372 0.89445 0.00000 0.00000 0.43318 0.89446

如您所见,我必须处理不同的分隔符(换行符、空格和逗号),并确保在读取字符串时它尊重引号并“忘记它们”(即它们最终不会成为结果字符串的一部分)。

在 Fortran 中,您可以编写如下内容:

read(*,*) dummyString, versionString
read(*,*) descriptoinString
read(*,*) type, order, code
read(*,*) listType, number, repeats
read(*,*) double1, double2, double3, double4
read(*,*) a1, a2, a3, a4, a5, a6, a7, a8

而生成的 C++ 则类似于:

stream >> dummyString >> versionString;
stream >> descriptoinString
stream >> type >> order >> code
stream >> listType >> number >> repeats
stream >> double1 >> double2 >> double3 >> double4
stream >> a1 >> a2 >> a3 >> a4 >> a5 >> a6 >> a7 >> a8

从我对 stringstream 的基本测试来看,它不能接受多个分隔符并保留引号。

最好的做法是什么?

I am looking for a way to mirror fortran read(*,*) functionality in C++ whilst reading fortran formatted files. I am familiar with, and would like to use a stream like syntax but I am not sure how to write my own/utilise a stream to do what I need. I have files with lines such as:

'Version' '5.3'
'This set has 1 member'
'Variable','nlo','EvolCode'
'list',1,1
1.E-05,1.,1.25,10000000.
0.00949 0.00208 -8.77372 0.89445 0.00000 0.00000 0.43318 0.89446

As you can see I have to deal with different delimiters (newline, space and comma) as well as making sure that when reading in strings it respects the inverted commas as well as "forgets them" (i.e. they don't end up as part of the resulting string).

In fortran you would write something like:

read(*,*) dummyString, versionString
read(*,*) descriptoinString
read(*,*) type, order, code
read(*,*) listType, number, repeats
read(*,*) double1, double2, double3, double4
read(*,*) a1, a2, a3, a4, a5, a6, a7, a8

Whereas the resulting C++ would be like:

stream >> dummyString >> versionString;
stream >> descriptoinString
stream >> type >> order >> code
stream >> listType >> number >> repeats
stream >> double1 >> double2 >> double3 >> double4
stream >> a1 >> a2 >> a3 >> a4 >> a5 >> a6 >> a7 >> a8

From my basic tests with stringstream, it can't accept multiple delimiters and keeps the inverted commas.

What is the best course of action?

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

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

发布评论

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

评论(1

ま柒月 2024-12-10 20:31:31

我通过实际利用 Fortran 设法解决了这个问题。这是有道理的,因为 Fortran 可以相对轻松地读取文件(并且以类似流的方式),并且这消除了能够读取封装在引号中的字符串的大问题。为了提供更完整的包装,我还使用 Fortran 文件单元读取文件。

如需完整实施,请参阅此处

I managed to solve this problem by actually leveraging Fortran. It made sense as Fortran can read the file with relative ease (and in a stream like manner), and this got rid of the big issue of being able to read strings encapsulated in inverted commas. To provide a fuller wrapper I also read in the file using fortran file units.

For a full implementation see here

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