English 中文(简体)
准备输入档案
原标题:Reading input files in FORTRAN
  • 时间:2010-05-11 18:43:46
  •  标签:
  • fortran

目的:创立一个包含两个单独档案、开放和阅读的方案,将其内容分配给各阵列,与这些阵列做一些数学,创建新的阵列,产品编号为新档案。 简单的权利?

我的投入文件从一开始就具有评论性质。 其中一个麻烦是,它们只是大多数策划方案的评论特征,但不是“国家培训”。 告诉计算机不要看这些特征的简单方法是什么? 自2006年以来 我没有以前曾经历过过过过培训,因此,我用两个测试文件来 p。 这就是我迄今为止所做的事情:

PROGRAM gain
  IMPLICIT NONE
  REAL, DIMENSION (1:4, 1:8)     :: X, Y, Z
  OPEN(1, FILE= test.out , &
        STATUS= OLD , ACTION= READ )            ! opens the first file
  READ(1,*), X
  OPEN(2, FILE= test2.out , &
    STATUS= OLD , ACTION= READ )            ! opens the second file
  READ(2,*), Y
  PRINT*, X, Y

  Z = X*Y
!  PRINT*, Z
  OPEN(3, FILE= test3.out , STATUS= NEW , ACTION= WRITE )   !creates a new file
  WRITE(3,*), Z
  CLOSE(1)
  CLOSE(2)
  CLOSE(3)
END PROGRAM

PS。 请不要把手法的nch子 go go。 我是全方位的方案拟订创新。 我不理解所有语气,这就是为什么我来到这里,而不是在现有网站上寻求帮助。 感谢。

最佳回答

撰写一条小路线,把这一逻辑放在一个位置上,以便你能够把这个逻辑放在这两个档案上。 你们需要阅读每一行,作为插图,并添加“综合框架”测试,以检查某一行是否开始使用“#”。 如果该行的起点是“第2号”,则改为下行。 否则,就会将扼杀转化为一种价值,并增加你们重新回归的各种价值。

问题回答

如果你表示这些评论只是档案一开始,那是相当简单的——没有必要算出评论线或更改档案——你可以把这些话线读到一个说明和检验中,看它们是否是评论。 然后,你最终会碰到一条不配对线。 问题:它会被解读为一种扼杀,因此无法定期阅读......解决办法......将“后天空”用于读一个记录,以便你现在能够使用正常档案读到其他档案。 如前所述,如果在整个档案中都使用过评论线,则需要稍加复杂的解决办法,然后从插图中读出。

这里是一个行之有效的例子。 我假定,“#”在第一栏和其他各种简化假设中。 一些建议:将贵处和职能纳入模块和“使用”模块——这将使汇编者能够核对接口。 在你制定方案时,将尽可能多地使用法律检查和警告办法——特别是规定约束检查——最终节省时间。

P.S.是正式“预备”的,因为第90段——是77年及更早的“FORTRAN”。

module read_file_module

   implicit none

contains

   subroutine read_file (UnitNum, FileName, NumRows, NumCols, Array )

      integer, intent (in) :: UnitNum
      character (len=*), intent (in) :: FileName
      integer, intent (in) :: NumRows, NumCols
      real, dimension (1:NumRows, 1:NumCols), intent (out) :: Array

      character (len=300) :: line
      integer :: i, j

      open (unit=UnitNum, file=FileName, status= old , action= read  )

      ReadComments: do
         read (UnitNum,  (A) ) line
         if (line (1:1) /= "#") exit ReadComments
      end do ReadComments

      backspace (UnitNum)

      do i=1, NumRows
         read (UnitNum, *) (Array (i, j), j=1,NumCols)
      end do

      close (UnitNum)

      return

   end subroutine read_file

end module read_file_module 




program test_prog

use read_file_module

implicit none

real, dimension (1:8, 1:4) :: Array
integer :: i, j

call read_file (66,  TestFile.txt , 8, 4, Array)

do i=1, 8
  write (*,  ( 4(2X, F7.3) )  ) (Array (i, j), j=1,4)
end do

end program test_prog

某些测试数据显示投入数据可如何灵活:

#  comment one
#  comment two
1.1   2.0  3.0  4.1
1.2   2.0  3.0  4.2
1.3   2.0  3.0  4.3
1.4   
  2.0  3.0  4.4
1.5   2.0  3.0  4.5
1.6   2.0  3.0  4.6


1.7   2.0  3.0  4.7
1.8   2.0  3.0  4.8

我并不真正熟悉“七七七十年”以外的任何东西,但这里是几个要点(以及你在你的回答中所描述的工作版本)。 第一,《工作法典》(我增加一行):

1   REAL FUNCTION myfile(unit, file, rows, columns)
2   IMPLICIT NONE
3   INTEGER, INTENT(IN) :: unit, rows, columns
4   CHARACTER(LEN=*) :: file
5   REAL, DIMENSION (1:columns, 1:rows) ::X
6   OPEN(unit, FILE=file, STATUS= OLD , ACTION= READ )
7   READ(unit,*), X
8   PRINT*, X         
9   CLOSE(unit)
10  myfile= 0
11  END FUNCTION myfile
12
13  PROGRAM gain
14  errno = myfile(1, "test.out", 8, 4)
15  END PROGRAM

差异是:

  • line 6 - You needed to remove the quotes around file in the FILE= file assignment. As written you were using a file named file instead of the name passed in as the file parameter.
  • line 10 - since you declared this as a function you need to assign a return value to the function name before you leave the function. I just assigned it a value of 0 to allow it to compile. I think what you want here is to pass the array you read in from the file back out of the routine. In that case you ll need to modify the type of the function (and assign X to myfile) or pass the array in as a parameter and allow it to be modified in the function. (in the old FORTRAN 77 world this was done with common blocks or pointers to the array, not sure how you do it in later versions of Fortran).
  • line 14 - you need to assign the return value of the function to a variable. At least you did with my gfortran compiler. It wouldn t let me compile the program otherwise.
  • line 14 (again) - the function call needs the name of the file (test.out) in quotes. You had it without quotes and so was having problems (this might be where your array reference error was coming from, I got different errors with my compiler.)

您不能使用一种功能,而是可以将我的档案例行公事定义为一种非常规做法。 在这种情况下,你显然需要通过你想要填充的参数。 你们不需要第10行,而不要把回报价值分配到你称之为常规的主要方案中的一个变量。 i.e. 第14行将照此办理:

call myfile(1, "test.out",8,4)

EDIT: I posted this and then realized I forgot to answer the original questions. I did so and then for some reason couldn t connect to SO to upload the edits. So here they are finally.

这是你例行汇编的。 为了实际处理评论线,你有几种选择(至少是最初想到的办法)。 这些要求从最简单/最精细的碎片到更强有力的/总体:

  1. If you know exactly how many comment lines are in the data file (and it is the same for all the files) you could just read in that many lines, throw away what was read in and then read in the array from that point. This will advance you past the comments and then read in the array. However if the number of comment lines is different in the different input files, this will not work. Likewise if it changes in the future it will require changing the code. This option is probably not the best.
  2. Make a pass through the file read a line at a time as a string and checking to see if it starts with a # mark. If so increment a counter. When you find the first non-comment line, stop, reset the file to the beginning and then follow the steps in #1 above where you use the counter value as the number of lines to skip. This is more flexible than #1 in that it can handle an arbitrary number of comment lines but they still have to be at the beginning of the file. Any comments in the middle of the data will mess you up.
  3. Read in each line as a string, look for the # symbol and if it is not there, parse the line and populate the array manually. This is the most complex but gives the most flexibility for the input file format. It allows you to have (and ignore) comments anywhere in the data file.

你们选择哪种方法(以及其他人可能还有其他建议)取决于你们的具体应用。 Good luck.





相关问题
How do I fix this Fortran text parser for Lapack within Gem5

After about an hour, I need to ask for some help. I ve downloaded the math benchmark Lapack to run within a few CPU configurations for the simulation tool Gem5, which lets you build X86 computers and ...

gfortran, DLL, underscore

I want to access some subroutines from a third party DLL. The functions use STDCALL as the calling convention. Running dumpbin /export foo.dll gives me something like: ... 7 6 ...

Getting started with a new code in an unfamiliar language

I m starting a new project in a language I m less familiar with (FORTRAN) and am in the discovery phase. Normally reading through and figuring out code is a fairly simple task, however, this code ...

assigning values to an integer array within a Fortran module

I have a module in Fortran called QFoo. It defines a type QFooType. I want to initialize all the elements of integer array is_n to 0 and want to do it within the module. Could someone help? Thank ...

WinDbg and Intel Visual Fortran

Has anyone used WinDbg to debug an Intel Visual Fortran routine? If I have the Fortran source file that crashes with an AccViol, how can I use WinDbg to determine the line that is crashing?

converting MATLAB code to Fortran [closed]

I a medical researcher with code written in MATLAB 2009b that runs very slowly because of a self-referential loop (not sure of the programming lingo here), i.e., the results of the first iteration is ...

热门标签