English 中文(简体)
读数
原标题:reading data from txt file in fortran

我正在撰写一份通辑节目,读到文本档案中的数据,并写给青少年。 数据档案中的情况就是如此。

1234567890123456 123456.789 987654.321 673647.890 654356.890
6172876534567890 768909.098 234543.890 654321.908 987890.090

我有以下几行的《外贸法》文本读取数据,只是写给青少年。

 OPEN(1,FILE= data.txt )
    READ(1, (I16,3F9.3) ) A ,B, C, D
    WRITE (*,  (I16,3F9.3) ) A,B,C,D
   CLOSE(1)

下面是产出,而不是在文本档案中显示同样的价值。

1234567890123456*********89987.656    0.322
6172876534567890*********98234.547    0.891

请帮助我这样做。

感谢

最佳回答

清单导向的IO(即*)更容易,特别是在投入方面。 然而,有时会充分利用国际独立实体的控制权,从而值得理解。 在投入方面,数据项目和说明必须按栏分列。 就投入而言,在Fw.d,如果你在数据项目中有点正确点,那么 matter就没有问题了。 这些领域在投入和产出方面必须足够广泛。 需要足够说明,其类型与变量和数据项目相匹配。 比较这一实例方案:

program test_read

   implicit none
   integer, parameter :: VLI_K = selected_int_kind (18)
   integer, parameter :: DR_K = selected_real_kind (14)

   integer (VLI_K) :: i
   real (DR_K) :: a, b, c, d

   open (unit=15, file="data.txt", status= old ,    &
             access= sequential , form= formatted , action= read  )

   read (15, 110)  i, a, b, c, d
   110 format (I16, 4(1X, F10.0) )
   write (*, 120) i, a, b, c, d
   120 format ( I18, 4 (2X, F12.3) )

   read (15, *) i, a, b, c, d
   write (*, 120) i, a, b, c, d

end program test_read
问题回答

I had the hardest time ever trying to use read, but finally... If you want to read a matrix stored in a .txt file use this:

program FILEREADER

   real, dimension(:,:), allocatable :: x
   integer :: n,m

   open (unit=99, file= array.txt , status= old , action= read )
   read(99, *), n
   read(99, *), m
   allocate(x(n,m))

   do I=1,n,1
      read(99,*) x(I,:)
      write(*,*) x(I,:)
   enddo

end

例如,“举报人”档案必须如此(安放在主人的同一部分):

4
3
0.0 1.0 2.0
3.0 4.0 5.0
6.0 7.0 8.0
9.0 10.0 11.0

Hope it works for everyone out there

Slight change to the Andrés Argüello Guillén questionnaire.

与大多数其他解决办法不同的是,我的法典并不强迫你事先具体说明行文和栏目的数量。

CHARACTER(128) :: buffer

integer strlen, rows, cols
real, dimension(:,:), allocatable :: x

OPEN (1, file =  matrix.txt , status= old , action= read )

!Count the number of columns

read(1, (a) ) buffer !read first line WITH SPACES INCLUDED
REWIND(1) !Get back to the file beginning

strlen = len(buffer) !Find the REAL length of a string read
do while (buffer(strlen:strlen) ==    ) 
  strlen = strlen - 1 
enddo

cols=0 !Count the number of spaces in the first line
do i=0,strlen
  if (buffer(i:i) ==    ) then
    cols=cols+1
  endif
enddo

cols = cols+1

!Count the number of rows

rows = 0 !Count the number of lines in a file
DO
  READ(1,*,iostat=io)
  IF (io/=0) EXIT
  rows = rows + 1
END DO

REWIND(1)

print*,  Number of rows: , rows
print*,  Number of columns: , cols

allocate(x(rows,cols))

do I=1,rows,1
  read(1,*) x(I,:)
  write(*,*) x(I,:)
enddo

CLOSE (1)

matrix.txt

0.0 1.0 2.0
3.0 4.0 5.0
6.0 7.0 8.0

I used fixed format because the editing and inspecting of input files having fixed column structure is easier than that of zigzag data. My problem was how the Fortran run-time reader procedures interpret the presence and absence of decimal dots. I am not sure that my solution was the best but I read the data lines as character arrays, split them to fields having length 12 characters then I read the fields by read(*) statements.

The reason is that you re specifying a width that is too small for the real numbers. Usually when the width doesn t fit, fortran will display asterisks, which happens in your case.

You have 9 digits, but you ll need at least 10, since the comma takes up a column as well. So replacing 3F9.3 with 3F10.3 should do the trick.

通常最好用非固定格式阅读数据。 离开某些主要空间,以便数字在书写时能够合适。

integer(8) :: i
real(4) :: x, y, z
open(unit=1, file= data.txt )
read(1,*)i, x, y, z
write(*, (i16, 3f11.3) )i, x, y, z
end




相关问题
Custom Formating a DateTimePicker using CultureInfo

I am using the DateTimePicker control in a Vb.Net Windows project. I would like the date to reflect the Regional Settings on the user s computer, but also to show the month name, rather than the ...

What settings storage format to choose?

I m writing a Qt application and will need to store the settings for the program. I want them to be easily editable by non-advanced users yet be flexible enough for advanced users (thus allow easy ...

Parsing date with timezone from an email?

I am trying to retrieve date from an email. At first it s easy: message = email.parser.Parser().parse(file) date = message[ Date ] print date and I receive: Mon, 16 Nov 2009 13:32:02 +0100 But I ...

Arduino String Formatting Issue

I m making an Arduino-powered clock, and in the process, I m trying to format integers into two-digit formatted strings for the time read-out (e.g. 1 into "01"). The following gives me "error: ...

How to get fields from such a string?

I m importing some data from a database. The data has been stored by a CMS written in php where I have no control. Here is the data (a dense report from a paypal response): a:56:{ s:8:"business";s:19:...

Nginx raises 404 when using format => js

I upload images to my App using Ajax and an Iframe. In Development everything works like a charm. But in production Nginx suddenly raises a 404 error. When I look into the log, the request never hits ...

Check if string is of SortableDateTimePattern format

Is there any way I can easily check if a string conforms to the SortableDateTimePattern ("s"), or do I need to write a regular expression? I ve got a form where users can input a copyright date (as a ...

热门标签