将整数传输到字符串 [英] Transfer integer to string

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

问题描述

我正在使用以下方法将整数传输到字符串

I am transfering an integer to a string using the following method

Character (len=10) :: s
Integer :: i
i = 7
Write (s, *, iostat=ios) i

但是,这将s的内容留为空白.当我引入显式格式时,将按预期填充s.

However this leaves the contents of s empty. When I introduce an explicit format, s is populated as intended.

Character (len=10) :: s
Integer :: i
i = 7
Write (s, "(i3)", iostat=ios) i

问题似乎与字符串s的长度有关,因为 当我使用更长的长度时,我确实得到了正确的数字.

It looks like the problem is related to the length of the string s because when I use a longer length, I do get the correct number.

Character (len=25) :: s

推荐答案

第一个教训是:如果使用iostat=说明符,请检查结果.但是,似乎无法保证代码的行为.我将在答案的这一部分中基于对编译器处理方式的理解.

The first lesson to take from this is: if you use the iostat= specifier, check the result. However, the behaviour of your code is seemingly not guaranteed. I'll base this part of the answer on intepreting how your compiler is taking things.

除了iostat=指示符外,通常您还可以使用iomsg=指示符来获得友好的消息,正如弗拉基米尔F在评论中提到的那样. [如IanH所指出的,仅在iostat=说明符的变量被设置为(或将被设置为非零的情况下),被提名的变量将被更新,并且特别地,否则可能保持未定义状态.]

In addition to the iostat= specifier, in general you can use, as Vladimir F mentions in a comment, the iomsg= specifier to get a friendly message. [As IanH notes, the nominated variable is updated, and in particular may otherwise remain undefined, only in situations where the variable for the iostat= specifier is (or would be) set to non-zero.]

character (len=10) s
character (len=58) mesg
integer ios

write(s,*, iostat=ios, iomsg=mesg) 7
if (ios/=0) print*, ios, TRIM(mesg)

您要检查此内容,因为您使用的是列表定向输出.在这里,编译器可以自由选择整数编辑格式的合理"值.对于默认整数类型,该字段很有可能会大于9(不要忘记开头的空格).因此,它不能适合"长度为10个字符的记录:在这种情况下,"<记录结束"对于mesg是合理的.

You want to check this, because you are using list-directed output. Here, the compiler is free to choose "reasonable" values for the integer edit format. It's more than likely that, for default integer kind, the field would be longer than 9 (don't forget the leading blank). Thus, it doesn't "fit" into the length-10 character record: "End of record" would be reasonable for mesg in this case.

使用显式格式I3,它可以容纳很多东西.

With the explicit format I3 it fits with much to spare.

为什么您将LEN_TRIM(s)视为10,是因为s实际上很可能变成了垃圾.

Why you see LEN_TRIM(s) as 10, is that s actually likely becomes junk.

现在,进入最后一部分.看来您的清单导向输出程式码无效. Fortran 2008(我想还有很多其他人)明确指出:

Now, coming to the final part. It appears that your code with list-directed output is not valid. Fortran 2008 (and I presume many others) explicitly states:

在输出时,输出列表和格式规范所指定的记录字符不得超过内部文件的记录长度.

On output, the output list and format specification shall not specify more characters for a record than ... the record length of an internal file.

内部文件的记录长度为10.

The record length of your internal file being 10.

依赖于任何特定行为的通常警告.但是,如果发生了一些戏剧性的事情,我会感到失望.

The usual caveats of relying on any particular behaviour hold. I'd be disappointed, though, if something dramatic happened.

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

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