在Fortran90中将整数转换为字符 [英] Converting integer to character in Fortran90

查看:357
本文介绍了在Fortran90中将整数转换为字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的程序中将一个整数转换为Fortran 90中的字符。
这是我的代码:

  Write(Array(i,j),'(I5)')Myarray(i,j)



< Array 是一个整数数组,而 Myarray 是一个字符数组,而'(I5 )',我不知道它是什么,之前只是为我工作!



错误是:


单元未打开,未预先连接


有时


<格式/数据不匹配>!


解决方案

Alexander Vogt解释 (I5)部分。这个答案也指出了一些其他问题并解决了主要问题。它并没有明确说明解决方案,所以我会在这里写出来。



你有两个错误,但都有相同的原因。

 写入(单位=数组(i,j) ,'(I5)')Myarray(i,j)

隐含的事情是单元= 。然后,您要求将字符变量 Myarray(i,j)写入与整数变量 Array(i ,j)



对于单位整数的某些值,文件未预先连接。你可能想阅读关于这个。当它不是你得到的第一个错误:


单位没有被打开没有预先连接
blockquote>

对于 Array(i,j)的某些值,例如5,6或其他取决于编译器的值,该单元将被预连接。然后,第一个错误不会发生,你会得到


格式/数据不匹配




,因为你试图用整数编辑描述符写出一个字符变量。



然后,这个答案是(Myarray(i,j),'(I5)')数组(b) i,j)

您希望将整数值写入字符变量。



最后,请注意,如果您使用真正的变量 array 而不是整数来犯同样的错误,那么您应该得到一个不同的错误信息。从某种意义上说,你的运气不好,你的语法是正确的,但意图是错误的。


I am trying to convert an integer to character in my program in Fortran 90. Here is my code:

Write(Array(i,j),'(I5)') Myarray(i,j)

Array is an integer array and Myarray is a character array, and '(I5)', I don't know what it is, just worked for me before!

Error is:

"Unit has neither been opened not preconnected"

and sometimes

"Format/data mismatch"!

解决方案

Alexander Vogt explains the meaning of the (I5) part. That answer also points out some other issues and fixes the main problem. It doesn't quite explicitly state the solution, so I'll write that here.

You have two errors, but both have the same cause. I'll re-state your write statement explicitly stating something which is implicit.

Write(unit=Array(i,j),'(I5)') Myarray(i,j)

That implicit thing is unit=. You are, then, asking to write the character variable Myarray(i,j) to the file connected to unit given by the integer variable Array(i,j).

For some values of the unit integer the file is not pre-connected. You may want to read about that. When it isn't you get the first error:

Unit has neither been opened not preconnected

For some values of Array(i,j), say 5, 6 or some other value depending on the compiler, the unit would be pre-connected. Then that first error doesn't come about and you get to

Format/data mismatch

because you are trying to write out a character variable with an integer edit descriptor.

This answer, then, is a long way of saying that you want to do

Write(Myarray(i,j),'(I5)') array(i,j)

You want to write the integer value to a character variable.

Finally, note that if you made the same mistake with a real variable array instead of integer, you would have got a different error message. In one way you just got unlucky that your syntax was correct but the intention was wrong.

这篇关于在Fortran90中将整数转换为字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆