正文

【原创】关于fortran读取各行长度不一格式不一具体行数不知数据文件2009-12-06 09:21:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/shenjinggege/50261.html

分享到:

iwanfly网友曾在一段时间前发过贴探讨过这个问题http://bbs.pfan.cn/post-312402.html当时思路不是很清晰现思考结果如下,虽然比较麻烦,但最终还是解决了读取数据的问题,前提是知道每行最大的数据个数,比如7个。不知道论坛上的网友是否有更好的方法,不吝赐教。测试数据如下:12  89  7712  999 878  777398 789 765 98 76578 8912  89  7712  999 878  777398 789 765 98 765。。。。总共78144行。大概需要5,6秒时间把数据全部读进。program mainimplicit noneinteger,allocatable :: int_array(:,:),temp_array(:,:)integer :: count,length,step,i,ierrcharacter*256 :: str_one_recordopen(100,file='data.txt')count=0step=100000length=stepallocate(int_array(length,7))int_array=0do while(.not. eof(100))    count=count+1    loop1:  do i=7,1,-1          read(100,'(A)',advance='no',eor=168) str_one_record168       read(str_one_record,*,end=999) int_array(count,1:i)            exit loop1999       backspace 100          int_array(count,1:i)=0          end do loop1        if(mod(count,step)==0) then        allocate(temp_array(length,7))        temp_array=0        temp_array=int_array        deallocate(int_array)        length=length+step        allocate(int_array(length,7))        int_array=0        int_array(1:length-step,:)=temp_array        deallocate(temp_array)    end if    end doallocate(temp_array(length,7))temp_array=0temp_array=int_arraydeallocate(int_array)allocate(int_array(count,7))int_array=0int_array(1:count,:)=temp_array(1:count,:)deallocate(temp_array)!下面处理int_array中数据deallocate(int_array)close(100)pauseend program mainiwanfly网友曾在一段时间前发过贴探讨过这个问题http://bbs.pfan.cn/post-312402.html当时思路不是很清晰现思考结果如下,虽然比较麻烦,但最终还是解决了读取数据的问题,前提是知道每行最大的数据个数,比如7个。不知道论坛上的网友是否有更好的方法,不吝赐教。测试数据如下:12  89  7712  999 878  777398 789 765 98 76578 8912  89  7712  999 878  777398 789 765 98 765。。。。总共78144行。大概需要5,6秒时间把数据全部读进。program mainimplicit noneinteger,allocatable :: int_array(:,:),temp_array(:,:)integer :: count,length,step,i,ierrcharacter*256 :: str_one_recordopen(100,file='data.txt')count=0step=100000length=stepallocate(int_array(length,7))int_array=0do while(.not. eof(100))    count=count+1    loop1:  do i=7,1,-1          read(100,'(A)',advance='no',eor=168) str_one_record168       read(str_one_record,*,end=999) int_array(count,1:i)            exit loop1999       backspace 100          int_array(count,1:i)=0          end do loop1        if(mod(count,step)==0) then        allocate(temp_array(length,7))        temp_array=0        temp_array=int_array        deallocate(int_array)        length=length+step        allocate(int_array(length,7))        int_array=0        int_array(1:length-step,:)=temp_array        deallocate(temp_array)    end if    end doallocate(temp_array(length,7))temp_array=0temp_array=int_arraydeallocate(int_array)allocate(int_array(count,7))int_array=0int_array(1:count,:)=temp_array(1:count,:)deallocate(temp_array)!下面处理int_array中数据deallocate(int_array)close(100)pauseend program main

阅读(4329) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册