在输入文件中缺少其中一个变量时从文件中读取已知数量的变量 [英] Reading a known number of variable from a file when one of the variables are missing in input file

查看:155
本文介绍了在输入文件中缺少其中一个变量时从文件中读取已知数量的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经检查过类似的帖子。解决方案是由MSB在这里

因此,我所遇到的问题有从文本文件,我正在尝试读取输入。在一行中应该有3个变量。但有时输入文件可能有2个变量。在这种情况下,我需要使最后一个变量为零。我尝试使用IOSTAT的READ语句,但是如果只有两个值,它会进入下一行并读取下一个可用的值。我没有第三个值的时候需要在第一行停下来。



我找到了一个方法来做一个评论/其他比我试图读取的类型(在这种情况下,我正在阅读浮动,而评论是一个字符),这使得IOSTAT> 0,我可以使用它作为一个检查。但是,如果在某些情况下,我可能不会有这样的评论。我想确保它的工作甚至比。

部分代码

  read(15,*)x 
read(15,*,IOSTAT = ioerr)y,z,w
if(ioerr.gt.0)then
write(*,*)'没有找到值'
W = 0.0;
goto 409
elseif(ioerr.eq.0)then
write(*,*)'Value found',w
endif
409 read(15,* )a,b
read(15,*)c,d

输入文件表格

  -1.000 abcd 
12.460 28.000 8.00 efg
5.000 5.000 hijk
20.000 21.000 lmno

即使没有8.00 efg,我也需要使它工作

$












$ b $
5.000 5.000 hijk
20.000 21.000 lmno

我不能使用字符串方法由MSB建议。有没有其他的方法?

解决方案

您可以使用奇妙的冒号编辑描述符。这允许您在I / O列表中没有其他项时跳过格式的其余部分:

 程序测试

隐含无

Real :: a,b,c
字符(Len = 10):: comment




c = 0.0
comment ='没有评论'
阅读(*,'(2(f7.3,1x),:,f7.3,a)') a,b,c,comment

Write(*,*)'I read',a,b,c,comment

End Do

结束程序测试

例如gfortran我得到:

 现在呢? gfortran -W -Wall -pedantic -std = f95 col.f90 
现在呢? ./a.out
12.460 28.000 8.00 efg
我读12.460000 28.000000 8.0000000 efg
12.460 28.000
我读12.460000 28.000000 0.00000000E + 00
^ C

这适用于gfortran,g95,NAG编译器,Intel编译器和Sun / Oracle编译器。不过,我应该说我并不完全相信,我明白这一点 - 如果C或评论不读,他们保证是0和所有空格分别?不确定,需要向别处询问。


I already checked similar posting. The solution is given by M. S. B. here Reading data file in Fortran with known number of lines but unknown number of entries in each line

So, the problem I am having is that from text file I am trying to read inputs. In one line there is supposed to be 3 variables. But sometimes the input file may have 2 variables. In that case I need to make the last variable zero. I tried using READ statement with IOSTAT but if there is only two values it goes to the next line and reads the next available value. I need to make it stop in the 1st line after reading 2 values when there is no 3rd value.

I found one way to do that is to have a comment/other than the type I am trying to read (in this case I am reading float while a comment is a char) which makes a IOSTAT>0 and I can use that as a check. But if in some cases I may not have that comment. I want to make sure it works even than.

Part of the code

    read(15,*) x
    read(15,*,IOSTAT=ioerr) y,z,w
    if (ioerr.gt.0) then
        write(*,*)'No value was found'
        w=0.0;
        goto 409
        elseif (ioerr.eq.0) then
        write(*,*)'Value found', w
        endif
  409   read(15,*) a,b
        read(15,*) c,d

INPUT FILE is of the form

    -1.000  abcd                                                                        
    12.460  28.000  8.00 efg                                                                            
    5.000   5.000   hijk                                                                            
    20.000  21.000  lmno                                                                            

I need to make it work even when there is no "8.00 efg"

for this case

    -1.000  abcd                                                                        
     12.460 28.000                                                                              
     5.000  5.000   hijk                                                                            
     20.000 21.000  lmno

I can not use the string method suggested by MSB. Is there any other way?

解决方案

You might be able to use the wonderfully named colon edit descriptor. This allows you to skip the rest of a format if there are no further items in the I/O list:

Program test

  Implicit None

  Real :: a, b, c
  Character( Len = 10 ) :: comment


  Do

     c = 0.0
     comment = 'No comment'
     Read( *, '( 2( f7.3, 1x ), :, f7.3, a )' ) a, b, c, comment

     Write( *, * ) 'I read ', a, b, c, comment

  End Do

End Program test

For instance with gfortran I get:

Wot now? gfortran -W -Wall -pedantic -std=f95 col.f90
Wot now? ./a.out
  12.460  28.000  8.00 efg
 I read    12.460000       28.000000       8.0000000     efg       
  12.460  28.000  
 I read    12.460000       28.000000      0.00000000E+00           
^C

This works with gfortran, g95, the NAG compiler, Intel's compiler and the Sun/Oracle compiler. However I should say I'm not totally convinced I understand this - if c or comment are NOT read are they guaranteed to be 0 and all spaces respectively? Not sure, need to ask elsewhere.

这篇关于在输入文件中缺少其中一个变量时从文件中读取已知数量的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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