| 查看: 866 | 回复: 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),像我这样写可以吗?? |
» 猜你喜欢
孩子确诊有中度注意力缺陷
已经有12人回复
2025冷门绝学什么时候出结果
已经有3人回复
天津工业大学郑柳春团队欢迎化学化工、高分子化学或有机合成方向的博士生和硕士生加入
已经有4人回复
康复大学泰山学者周祺惠团队招收博士研究生
已经有6人回复
AI论文写作工具:是科研加速器还是学术作弊器?
已经有3人回复
2026博士申请-功能高分子,水凝胶方向
已经有6人回复
论文投稿,期刊推荐
已经有4人回复
硕士和导师闹得不愉快
已经有13人回复
请问2026国家基金面上项目会启动申2停1吗
已经有5人回复
同一篇文章,用不同账号投稿对编辑决定是否送审有没有影响?
已经有3人回复

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













回复此楼