| 查看: 881 | 回复: 24 | |||
| 当前主题已经存档。 | |||
[交流]
【求助】新手求助,不知道文件中数据有多少行怎么半?
|
|||
|
新手求助!! 我要写一段程序,读取文件中的每一行数据,调用子程序进行计算,可是我并不知道这个文件有多少行,这段程序应该怎么写,请大家帮忙... program main implicit none integer i, nrow ! nrow是文件的行数 character*10 atomname open(1,file='co2.gro',status='unknown') do i=1, nrow !读取文件中的每一行数据,可是我并不知道这个nrow的具体数值. read(1,*) atomname,x(i),y(i),z(i) if (atomname .eq. 'cb') then call sub(x(i),y(i),z(i)) 请大家帮忙,另外我想读取每一行数据后,调用子程序进行运算,我想知道把文件中的每一行数值赋值给子程序sub(x,y,z),像我这样写可以吗?? |
» 猜你喜欢
求助:我三月中下旬出站,青基依托单位怎么办?
已经有9人回复
Cas 72-43-5需要30g,定制合成,能接单的留言
已经有8人回复
北京211副教授,35岁,想重新出发,去国外做博后,怎么样?
已经有8人回复
磺酰氟产物,毕不了业了!
已经有5人回复
论文终于录用啦!满足毕业条件了
已经有25人回复
2026年机械制造与材料应用国际会议 (ICMMMA 2026)
已经有3人回复
自荐读博
已经有3人回复
不自信的我
已经有5人回复
投稿Elsevier的杂志(返修),总是在选择OA和subscription界面被踢皮球
已经有8人回复

maomao1210
金虫 (正式写手)
- 程序强帖: 5
- 应助: 2 (幼儿园)
- 金币: 1431.3
- 散金: 242
- 红花: 16
- 沙发: 1
- 帖子: 991
- 在线: 441.5小时
- 虫号: 253215
- 注册: 2006-05-20
- 性别: MM
- 专业: 考古理论
2楼2009-11-17 16:19:06
3楼2009-11-17 16:51:18

4楼2009-11-17 17:02:53
maomao1210
金虫 (正式写手)
- 程序强帖: 5
- 应助: 2 (幼儿园)
- 金币: 1431.3
- 散金: 242
- 红花: 16
- 沙发: 1
- 帖子: 991
- 在线: 441.5小时
- 虫号: 253215
- 注册: 2006-05-20
- 性别: MM
- 专业: 考古理论
5楼2009-11-17 19:35:32
senlia
木虫 (小有名气)
- 应助: 2 (幼儿园)
- 贵宾: 3.521
- 金币: 2178.1
- 红花: 1
- 帖子: 223
- 在线: 6.7小时
- 虫号: 849005
- 注册: 2009-09-16
- 性别: GG
- 专业: 计算数学与科学工程计算

6楼2009-11-17 20:25:03
tjyl
金虫 (正式写手)
- 程序强帖: 2
- 应助: 0 (幼儿园)
- 金币: 3218.1
- 红花: 2
- 帖子: 576
- 在线: 156.6小时
- 虫号: 765184
- 注册: 2009-05-07
- 专业: 生物无机化学
- 管辖: 程序语言
7楼2009-11-18 13:03:59
tjyl
金虫 (正式写手)
- 程序强帖: 2
- 应助: 0 (幼儿园)
- 金币: 3218.1
- 红花: 2
- 帖子: 576
- 在线: 156.6小时
- 虫号: 765184
- 注册: 2009-05-07
- 专业: 生物无机化学
- 管辖: 程序语言
★ ★
nono2009(金币+2,VIP+0):谢谢应助! 11-18 13:21
nono2009(金币+2,VIP+0):谢谢应助! 11-18 13:21
|
使用函数EOF。 eof(file_id)如果为真则说明到达文件末尾了,就不读了。 下面是编译器手册上的说明和范例: EOF Inquiry Intrinsic Function (Generic): Checks whether a file is at or beyond the end-of-file record. Syntax result = EOF (unit) unit (Input) Must be of type integer. It represents a unit specifier corresponding to an open file. It cannot be zero unless you have reconnected unit zero to a unit other than the screen or keyboard. Results The result type is default logical. The value of the result is .TRUE. if the file connected to unit is at or beyond the end-of-file record; otherwise, .FALSE.. Example ! Creates a file of random numbers, reads them back REAL x, total INTEGER count OPEN (1, FILE = 'TEST.DAT') DO I = 1, 20 CALL RANDOM_NUMBER(x) WRITE (1, '(F6.3)') x * 100.0 END DO CLOSE(1) OPEN (1, FILE = 'TEST.DAT') DO WHILE (.NOT. EOF(1)) count = count + 1 READ (1, *) value total = total + value END DO 100 IF ( count .GT. 0) THEN WRITE (*,*) 'Average is: ', total / count ELSE WRITE (*,*) 'Input file is empty ' END IF STOP END See Also ENDFILE BACKSPACE REWIND |
8楼2009-11-18 13:05:37
tjyl
金虫 (正式写手)
- 程序强帖: 2
- 应助: 0 (幼儿园)
- 金币: 3218.1
- 红花: 2
- 帖子: 576
- 在线: 156.6小时
- 虫号: 765184
- 注册: 2009-05-07
- 专业: 生物无机化学
- 管辖: 程序语言
★ ★
nono2009(金币+2,VIP+0):谢谢应助! 11-18 13:21
nono2009(金币+2,VIP+0):谢谢应助! 11-18 13:21
|
program main implicit none integer i, nrow ! nrow是文件的行数 character*10 atomname open(1,file='co2.gro',status='unknown') i=1 do while (.not. eof(1)) read(1,*) atomname,x(i),y(i),z(i) if (atomname .eq. 'cb') then call sub(x(i),y(i),z(i)) i=i+1 end do end program 没有测试过,自己试试吧。 [ Last edited by tjyl on 2009-11-18 at 13:10 ] |
9楼2009-11-18 13:08:55
maomao1210
金虫 (正式写手)
- 程序强帖: 5
- 应助: 2 (幼儿园)
- 金币: 1431.3
- 散金: 242
- 红花: 16
- 沙发: 1
- 帖子: 991
- 在线: 441.5小时
- 虫号: 253215
- 注册: 2006-05-20
- 性别: MM
- 专业: 考古理论
10楼2009-11-18 13:19:01









回复此楼