有没有一种简单的方法可以在 FORTRAN 中读取文件并打印到标准输出?
我有一个数据文件想要打印到标准输出。这是否可以在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以打开文件进行流访问,并在循环中一次处理一个字符,直到到达文件末尾。我不知道这对于大型数据文件来说有多高效,但它使您不必定义一个长度足以容纳最长行的字符变量,这需要有根据的猜测。
我已经使用了两个 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.
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 theerror
part if needed.好的,我只是有点好奇你在哪里使用 Fortran。
您可以将其读入循环内的字符变量中。 Fortran 有一个声明,比如
您可以继续阅读直到到达 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
where you can continue reading until you reach and EOF (and it will jump to line 10 on EOF).