声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 3727|回复: 3

[Fortran] [请教]在fortran中读取ASCII码数据文件时...

[复制链接]
发表于 2006-9-20 09:43 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
在fortran中读取ASCII码数据文件时,如何让程序从文件的指定行开始读?
回复
分享到:

使用道具 举报

发表于 2006-9-20 10:11 | 显示全部楼层
查fortran书
发表于 2007-1-1 22:45 | 显示全部楼层
原帖由 abcdefg100 于 2006-9-20 09:43 发表
在fortran中读取ASCII码数据文件时,如何让程序从文件的指定行开始读?


遇到同样问题,不知道该如何实现?

每行的字符数不一样多,不知道如何实现从行首读到行尾?(预先定义字符长度为定值的方法除外,这种方法先暂时用着,应付着...)

能不能让程序自动判断每行的行尾所在的列数,然后返回一个数值,之后才定义字符变量的长度,读入数组?貌似难度大了一些..... :D
发表于 2007-1-2 13:09 | 显示全部楼层
(建议版主把这个帖子移动到编程版fortran专题下)

下面的方法虽然解决不了我的问题,不过也是很有价值的。

From:    http://www.tek-tips.com/viewthread.cfm?qid=1315394&page=1

How to read data from specific line in a file  


sougandh (TechnicalUser)         
21 Dec 06 12:11
Hi,
    Here is my problem. My file is in the following format.
    It has 5 columns with first two being integers and 3-5 are  floating values.

   file format (similar format with different values)

      col 1     col 2       col 3    col 4     col 5

     5001       1001        12.34   11.34    43.454
     5001       1002        11.54   65.43    65.234
     5001       1003        12.54   54.64    65.778
     .          .           .       .        .
     .          .           .       .        .
     5001       1099        14.65   16.53    12.568
     5001       1100        13.65   13.54    12.545     
     5002       1001        12.34   11.34    43.454
     5002       1002        11.54   65.43    65.234
     5002       1003        12.54   54.64    65.778
     .          .           .       .        .
     .          .           .       .        .
     5002       1099        13.45   51.15    61.542
     5002       1100        13.65   13.54    12.545
     
     so on...

     That means, for each number in column 1 i have 100 values. Or i should say the column 1 number doesnt change for for 100 rows and then it increases. Infact  my original file has (5760 x 4633) rows.

    Now here is how i need to read it:

There is a variable 'crhr' which returns a value which corresponds to any of the values in column 1. For example it may be 5002 or 5050 or anything. So when this variable has a value of 5002 what i need is i should be able to read the rows corresponding to the number 5002 and store it in an array.

  My problem now is how do i make Fortran start reading the file from a particular line based on the value returned by the variable 'crhr'


thanks
sougandh

-----------------------------------------------------
Helpful Member!xwb (Programmer)         
21 Dec 06 16:30
Are all the lines the same size (in characters).  If so, then you can use formatted reads on direct access files
CODE
program main
   integer FHAND, TMAX, RMAX
   parameter (TMAX=10, RMAX=5, FHAND=88)
   real t(TMAX)
   integer reqd(RMAX)
   data reqd/ 1, 2, 16, 8, 4 /
      
   ! file - the data file
   ! status - always old
   ! form - formatted for human readable data
   !      - unformatted for binary data
   ! access - always direct.  The alternative is sequential
   ! recl - number of characters incl LF
   open (FHAND, &
      file='data.txt', &
      status='old', &
      form='FORMATTED', &
      access='direct', &
      recl=51)
   do i = 1, RMAX
      ! Read record reqd(i)
      read (FHAND, '(10F5.1)', rec=reqd(i)) (t(j), j = 1, TMAX)
      write (*, '(10F5.1)') (t(j), j = 1, TMAX)
   enddo
   close (FHAND)
   stop
end program
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-5-18 07:23 , Processed in 0.052944 second(s), 17 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表