Fortran中处理字符串数字和文件的模块StrNum

来源:互联网 发布:任务网源码 编辑:程序博客网 时间:2024/05/24 05:04

花了3天多的时间,写了将近千行的Fortran代码,起初主要是想能否用Fortran轻松地读取文本文件中的数据,

所以写了StrNum的模块:该模块提供字符串,数字和文本文件处理的功能子程序.

注意保存代码到文件的时候选择UTF8格式.测试环境MinGW::gfortran 4.7.1,win 7.

下面的程序从命令行读入文件名,然后将文件中的数据存入二维数组,接着在屏幕上打印出来,测试程序如下:

include"StrNum.F90"!--------------------------------------------------program mainuse StrNumimplicit none!--------------------------------------------------integer :: count,i,jCHARACTER(len=24) :: Filename !命令行参数Integer::Error,HeadLine,Row,Columnreal(kind=8), allocatable :: Array2D(:,:)!-----------------------------------------------------------------count = command_argument_count() !获取主程序命令行的输入参数的个数!------------------------------------------------------------------if (count>0) thendo i=1,countCALL get_command_argument(i, Filename)write(*,*)'------------------------------------------'call GetFileRowColumn(Filename,HeadLine,Row,Column)write(*,*)"HeadLine=",HeadLine,"Row=",Row,"Column=",Columnwrite(*,*)'------------------------------------------'call LoadFromFile(Filename,Array2D,Row,Column,Error)if (Error==0)thenwrite(*,*)"Array2D(Row,Column):"do j=1,Rowwrite(*,*)Array2D( j,1:Column)end dowrite(*,*)'------------------------------------------'elsewrite(*,*)"文件读写有误"end ifend doelse    write(*,*) 'You should input an argument!'end if!---------------------------------------------if (Allocated(Array2D)) thendeallocate(Array2D)end if!---------------------------------------------end program


上面的程序可以很方便地读出文本文件中的数据,而且无须知道文件中数据的存放格式,

每行和每列的数据个数可以不一样,缺少的数据自动补为0.0,

自动识别文件头等信息,输出结果如下:


模块StrNum.F90的代码如下:

!------------------------------------------------------------!---StrNum.F90:提供字符串,数字和文本文件处理的功能子程序!---吴徐平2013-07-22(wxp07@qq.com)!------------------------------------------------------------module StrNum!----------------------------------------------implicit none!----------------------------------------------!---字符串转换为数字数StrToNum(InStr,Num,Error)interface StrToNummodule procedure StrToReal4module procedure StrToReal8module procedure StrToInteger1module procedure StrToInteger2module procedure StrToInteger4end interface!----------------------------------------------!---保留InStr中的浮点数相关的字符,其它字符全部变为空格KeepRealChar(InStr)interface KeepRealCharmodule procedure KeepRealCharend interface!----------------------------------------------!---保留InStr中的浮点数相关的字符,其它字符全部变为空格KeepNumChar(InStr)interface KeepNumCharmodule procedure KeepNumCharend interface!----------------------------------------------!---识别InStr中左右有效可见字符(33-126)的索引TrimIndex(InStr,LeftIndex,RightIndex,Error)interface TrimIndexmodule procedure TrimIndexend interface!----------------------------------------------!---字符串分割StringSplit(InStr,delimiter,StrArray,nsize)interface StringSplitmodule procedure StringSplitend interface!----------------------------------------------!---字符串替换StrReplace(InStr,OldChar,NewChar,OutStr)interface StrReplacemodule procedure StrReplaceend interface!----------------------------------------------!---字符串变为浮点数组StrToRealArray(InStr,RealArray,nsize)interface StrToRealArraymodule procedure StrToRealArray4module procedure StrToRealArray8end interface!----------------------------------------------!---测试字符串是否可以转为RealArray数组:IsRealArrayString(InStr,Error)interface IsRealArrayStringmodule procedure IsRealArrayStringend interface!----------------------------------------------!---获取文本文件FileName行列信息:GetFileRowColumn(FileName,HeadLine,Row,Column)interface GetFileRowColumnmodule procedure GetFileRowColumnend interface!----------------------------------------------!---文件数据的读取LoadFromFile(FileName,Array2D,Row,Column,Error)interface LoadFromFilemodule procedure LoadFromFile4module procedure LoadFromFile8end interface!----------------------------------------------!**********************************************************contains!**********************************************************!=============================================================subroutine StrToReal4(InStr,Num,Error)!------------------------------------------------------------!---将字符串InStr转为Num数字类型!---如果Error == 0 ::表示InStr可以转为Num,否则转换错误!---吴徐平2013-07-20(wxp07@qq.com)!------------------------------------------------------------Implicit NoneCharacter(Len = *), Intent( IN ) :: InStrCharacter(Len = LEN(InStr)):: Str_tempReal(kind = 4),Intent( INOUT ) :: NumInteger,Intent( INOUT ) :: ErrorInteger::LeftIndex,RightIndex!-----------------Num=0Error=0!-----------------if (LEN(TRIM(InStr))>0 ) thenStr_temp=InStr !为了不修改原始字符串的内容call KeepRealChar(Str_temp) !只保留浮点数相关的字符call TrimIndex(Str_temp,LeftIndex,RightIndex,Error)!-----------------if (Error==0 ) thenRead( Str_temp(LeftIndex:RightIndex) , * ,iostat=Error) NumelseError=Error+1end ifelseError=Error+1end if!-----------------end subroutine StrToReal4!!=============================================================subroutine StrToReal8(InStr,Num,Error)!------------------------------------------------------------!---将字符串InStr转为Num数字类型!---如果Error == 0 ::表示InStr可以转为Num,否则转换错误!---吴徐平2013-07-20(wxp07@qq.com)!------------------------------------------------------------Implicit NoneCharacter(Len = *), Intent( IN ) :: InStrCharacter(Len = LEN(InStr)):: Str_tempReal(kind = 8),Intent( INOUT ) :: NumInteger,Intent( INOUT ) :: ErrorInteger::LeftIndex,RightIndex!-----------------Num=0Error=0!-----------------if (LEN(TRIM(InStr))>0 ) thenStr_temp=InStr !为了不修改原始字符串的内容call KeepRealChar(Str_temp) !只保留浮点数相关的字符call TrimIndex(Str_temp,LeftIndex,RightIndex,Error)!-----------------if (Error==0 ) thenRead( Str_temp(LeftIndex:RightIndex) , * ,iostat=Error) NumelseError=Error+1end if!-----------------elseError=Error+1end ifend subroutine StrToReal8!!=============================================================!=============================================================subroutine StrToInteger1(InStr,Num,Error)!------------------------------------------------------------!---将字符串InStr转为Num数字类型!---如果Error == 0 ::表示InStr可以转为Num,否则转换错误!---吴徐平2013-07-20(wxp07@qq.com)!------------------------------------------------------------Implicit NoneCharacter(Len = *), Intent( IN ) :: InStrCharacter(Len = LEN(InStr)):: Str_tempInteger(kind = 1),Intent( INOUT ) :: NumInteger,Intent( INOUT ) :: ErrorInteger::LeftIndex,RightIndex!-----------------Num=0Error=0!-----------------if (LEN(TRIM(InStr))>0 ) thenStr_temp=InStr !为了不修改原始字符串的内容call KeepRealChar(Str_temp) !只保留浮点数相关的字符call TrimIndex(Str_temp,LeftIndex,RightIndex,Error)!-----------------if (Error==0 ) thenRead( Str_temp(LeftIndex:RightIndex) , * ,iostat=Error) NumelseError=Error+1end if!-----------------elseError=Error+1end ifend subroutine StrToInteger1!!=============================================================subroutine StrToInteger2(InStr,Num,Error)!------------------------------------------------------------!---将字符串InStr转为Num数字类型!---如果Error == 0 ::表示InStr可以转为Num,否则转换错误!---吴徐平2013-07-20(wxp07@qq.com)!------------------------------------------------------------Implicit NoneCharacter(Len = *), Intent( IN ) :: InStrCharacter(Len = LEN(InStr)):: Str_tempInteger(kind = 2),Intent( INOUT ) :: NumInteger,Intent( INOUT ) :: ErrorInteger::LeftIndex,RightIndex!-----------------Num=0Error=0!-----------------if (LEN(TRIM(InStr))>0 ) thenStr_temp=InStr !为了不修改原始字符串的内容call KeepRealChar(Str_temp) !只保留浮点数相关的字符call TrimIndex(Str_temp,LeftIndex,RightIndex,Error)!-----------------if (Error==0 ) thenRead( Str_temp(LeftIndex:RightIndex) , * ,iostat=Error) NumelseError=Error+1end if!-----------------elseError=Error+1end if!-----------------end subroutine StrToInteger2!!=============================================================subroutine StrToInteger4(InStr,Num,Error)!------------------------------------------------------------!---将字符串InStr转为Num数字类型!---如果Error == 0 ::表示InStr可以转为Num,否则转换错误!---吴徐平2013-07-20(wxp07@qq.com)!------------------------------------------------------------Implicit NoneCharacter(Len = *), Intent( IN ) :: InStrCharacter(Len = LEN(InStr)):: Str_tempInteger(kind = 4),Intent( INOUT ) :: NumInteger,Intent( INOUT ) :: ErrorInteger::LeftIndex,RightIndex!-----------------Num=0Error=0!-----------------if (LEN(TRIM(InStr))>0 ) then!-----------------Str_temp=InStr !为了不修改原始字符串的内容call KeepRealChar(Str_temp) !只保留浮点数相关的字符call TrimIndex(Str_temp,LeftIndex,RightIndex,Error)!-----------------if (Error==0 ) thenRead( Str_temp(LeftIndex:RightIndex) , * ,iostat=Error) NumelseError=Error+1end if!-----------------elseError=Error+1end ifend subroutine StrToInteger4!!=============================================================subroutine KeepRealChar(InStr)!------------------------------------------------------------!---保留InStr中的浮点数相关的字符,其它字符全部变为空格!---吴徐平2013-07-20(wxp07@qq.com)!------------------------------------------------------------Implicit NoneCharacter(Len =*),Intent( INOUT ) :: InStrCharacter(Len =17):: RealChar='+-.0123456789eEdD'Character(Len =4):: StartChar='eEdD'Character(Len =6):: EndChar='eEdD+-'Character(Len =7):: SingleChar='eEdD+-.'!------------------------------------------------------------Integer ::i,j,k,flag,Error!------------------------------------------------------------do i=1,LEN(InStr)  flag=0  !-------------------------------do j=1,LEN(RealChar)!-------------------------------if (InStr(i:i)==RealChar(j:j)) thenflag=flag+1  !-识别为RealChar浮点数字符Exitend if!------------------------------end do!-------------------------------if (flag==0) thenInStr(i:i)=' '  !-非RealChar浮点数字符,置为空格end if!------------------------------end do!------------------------------------------------------------!---第一个有效字符不能为StartChar='eEdD'do while(.TRUE.)  call TrimIndex(InStr,i,j,Error)if (Error==0)then  !-------------------------------  flag=0  !------------------------------do k=1,LEN(StartChar)!-------------------------------if (InStr(i:i)==StartChar(k:k)) thenflag=flag+1  !-第一个有效字符不能为eEdDExitend if!------------------------------end do!------------------------------if (flag>0)thenInStr(i:i)=' ' !将该字符置为空格elseEXIT!-第一个有效字符不是eEdDend if!------------------------------elseEXITend ifend do!------------------------------------------------------------!---最后一个有效字符不能为EndChar='eEdD+-'do while(.TRUE.)  call TrimIndex(InStr,i,j,Error)if (Error==0)then  !-------------------------------  flag=0  !------------------------------do k=1,LEN(EndChar)!-------------------------------if (InStr(j:j)==EndChar(k:k)) thenflag=flag+1  !-最后一个有效字符不能为EndChar='eEdD+-'Exitend if!------------------------------end do!------------------------------if (flag>0)thenInStr(j:j)=' ' !将该字符置为空格elseEXIT!-最后一个有效字符不是EndChar='eEdD+-'end if!------------------------------elseEXITend ifend do!------------------------------------------------------------!---如果只含有一个有效字符,则不能是SingleChar='eEdD+-.'do while(.TRUE.)  call TrimIndex(InStr,i,j,Error)if ((Error==0) .AND. (i==j))then  !-------------------------------  flag=0  !------------------------------do k=1,LEN(SingleChar)!-------------------------------if (InStr(i:i)==SingleChar(k:k)) thenflag=flag+1  !-有效字符不能为SingleCharExitend if!------------------------------end do!------------------------------if (flag>0)thenInStr(i:i)=' ' !将该字符置为空格elseEXIT!-有效字符不是SingleCharend if!------------------------------elseEXITend ifend do!------------------------------------------------------------end subroutine KeepRealChar!=============================================================subroutine KeepNumChar(InStr)!------------------------------------------------------------!---保留InStr中的数字字符,其它字符全部变为空格!---吴徐平2013-07-20(wxp07@qq.com)!------------------------------------------------------------Implicit NoneCharacter(Len =*),Intent( INOUT ) :: InStrCharacter(Len =10):: NumChar='0123456789'!------------------------------------------------------------Integer ::i,j,flag!------------------------------------------------------------do i=1,LEN(InStr)  flag=0  !-------------------------------do j=1,LEN(NumChar)!-------------------------------if (InStr(i:i)==NumChar(j:j)) thenflag=flag+1  !-识别为NumChar字符Exitend if!------------------------------end do!-------------------------------if (flag==0) thenInStr(i:i)=' '  !-非NumChar字符,置为空格end if!------------------------------end do!------------------------------------------------------------end subroutine KeepNumChar!=============================================================subroutine TrimIndex(InStr,LeftIndex,RightIndex,Error)!------------------------------------------------------------!---识别InStr中左右有效可见字符(33-126)的索引!---如果Error==0,则识别正确!---吴徐平2013-07-20(wxp07@qq.com)!------------------------------------------------------------Implicit NoneCharacter(Len =*),Intent( IN ) :: InStrInteger,Intent( OUT)::LeftIndex,RightIndex,Error!------------------------------------------------------------Integer ::iLeftIndex=0RightIndex=LEN(InStr)+1!------------------------------------------------------------if (LEN(TRIM(InStr))>0) thendo i=1,LEN(InStr),1if ((IACHAR(InStr(i:i)) >32 ).AND.(IACHAR(InStr(i:i)) <127) ) thenLeftIndex=i !-左边有效可见字符(33-126)的索引EXITend ifend do!------------------------------------------------------------do i=LEN(InStr),1,-1if ((IACHAR(InStr(i:i)) >32 ).AND.(IACHAR(InStr(i:i)) <127 )) thenRightIndex=i !-右边有效可见字符(33-126)的索引EXITend ifend do!--------------------------if ((LeftIndex>0 ).AND. (LeftIndex<=RightIndex) .AND. (RightIndex<=LEN(InStr)))thenError=0  !-操作正确elseError=-1 !-操作有误end if!--------------------------elseError=-1 !-字符串全部为空格或是空字符串end ifend subroutine TrimIndex!=============================================================subroutine StringSplit(InStr,delimiter,StrArray,nsize)!----------------------------------------------!---将字符串InStr进行分割,结果放入StrArray中!---delimiter::分隔符号,例如';,,' 使用;和,分割字符串!---nsize:分割数目!---吴徐平2011-04-29(wxp07@qq.com)!----------------------------------------------implicit nonecharacter(len = *) , Intent( IN ) :: InStrcharacter(len = *)  , Intent( IN ) :: delimitercharacter(len = LEN(InStr)),dimension(LEN(InStr)),Intent( OUT ) :: StrArrayinteger, Intent( OUT ) :: nsize ! Effective Size of StrArrayinteger:: i,j ! loop variableinteger:: istart ! split index for Start Positionnsize=0istart=1do i=1,LEN(InStr)do j=1,LEN(delimiter)if (InStr(i:i) == delimiter(j:j)) thenif (istart == i) thenistart=i+1 ! ---可防止分隔符相连的情况end ifif (istart<i) thennsize=nsize+1StrArray(nsize)=InStr(istart:i-1)istart=i+1end ifend ifend doend do! ---匹配最后一个子字符串if (nsize>0) thenif (istart<LEN(InStr)) thennsize=nsize+1StrArray(nsize)=InStr(istart:LEN(InStr))end ifend if! ---如果无可分割的子字符串,则包含整个字符串为数组的第一元素if ( (nsize<1) .AND. (LEN(TRIM(InStr)) > 0 )) thennsize=1StrArray(1)=InStrend ifend subroutine StringSplit!!=============================================================subroutine StrReplace(InStr,OldChar,NewChar,OutStr)!------------------------------------------------------------!---将字符串InStr中的字符串OldChar替换成NewChar!---结果放入字符串OutStr中!---吴徐平2013-07-20(wxp07@qq.com)!------------------------------------------------------------implicit nonecharacter(len = *) , Intent( IN ) :: InStrcharacter(len = *) , Intent( IN ) :: OldCharcharacter(len = LEN(OldChar)) , Intent( IN ) ::NewCharcharacter(len = LEN(InStr)) , Intent( INOUT ) :: OutStrinteger :: i  ! loop variableOutStr=InStri=INDEX(OutStr,OldChar)do while(i>0)OutStr(i:i+LEN(OldChar)-1)=NewChari=INDEX(OutStr,OldChar)end doend subroutine StrReplace!------------------------------------------------------------!=============================================================subroutine StrToRealArray4(InStr,RealArray,nsize)!------------------------------------------------------------Implicit NoneCharacter(Len = *), Intent( IN ) :: InStrCharacter(Len = LEN(InStr)):: Str_tempInteger:: i,j,Error,nsizeReal::Numcharacter(len =LEN(InStr)),dimension(LEN(InStr)):: StrArrayReal(kind=4),dimension(LEN(InStr)),Intent(OUT) :: RealArraycharacter(len = 4):: delimiter=' ;,'Error=0nsize=0j=0Str_temp=InStrcall KeepRealChar(Str_temp)!----------------------call  StringSplit(Str_temp,delimiter,StrArray,nsize)if (nsize>=1)thendo i=1,nsize  call KeepRealChar(StrArray(i))call StrToNum(StrArray(i),Num,Error)if (Error==0) thenj=j+1RealArray(j)=Numend ifend doend ifnsize=j!------------------------------------------------------end subroutine StrToRealArray4!=============================================================subroutine StrToRealArray8(InStr,RealArray,nsize)!------------------------------------------------------------Implicit NoneCharacter(Len = *), Intent( IN ) :: InStrCharacter(Len = LEN(InStr)):: Str_tempInteger:: i,j,Error,nsizeReal::Numcharacter(len =LEN(InStr)),dimension(LEN(InStr)):: StrArrayReal(kind=8),dimension(LEN(InStr)),Intent(OUT) :: RealArraycharacter(len = 4):: delimiter=' ;,'Error=0nsize=0j=0Str_temp=InStrcall KeepRealChar(Str_temp)!----------------------call  StringSplit(Str_temp,delimiter,StrArray,nsize)if (nsize>=1)thendo i=1,nsize  call KeepRealChar(StrArray(i))  write(*,*)StrArray(i)call StrToNum(StrArray(i),Num,Error)if (Error==0) thenj=j+1RealArray(j)=Numend ifend doend ifnsize=j!------------------------------------------------------end subroutine StrToRealArray8!=============================================================!=============================================================subroutine IsRealArrayString(InStr,Error)!------------------------------------------------------------!---测试字符串InStr转为RealArray类型的数组!---Error == 0 ::表示InStr可以转为RealArray数组,否则不能转换!---吴徐平2011-04-29(wxp07@qq.com)!------------------------------------------------------------Implicit NoneCharacter(Len = *), Intent( IN ) :: InStrInteger ,Intent( OUT ) :: ErrorReal,dimension(LEN(InStr)):: RealArray!------------------------------------------Integer::nsizeError=0nsize=0!------------------------------------------call StrToRealArray(InStr,RealArray,nsize)if (nsize>=1)thenError=0 !可以转为RealArrayelseError=-1 !不可以转为RealArrayend if!------------------------------------------------------end subroutine IsRealArrayString!!=============================================================subroutine GetFileRowColumn(FileName,HeadLine,Row,Column)!------------------------------------------------------------!---获取文本文件FileName的行数Row!---吴徐平2013-07-20(wxp07@qq.com)!------------------------------------------------------------Implicit NoneCharacter(Len = *), Intent( IN ) :: FileNameInteger, Intent( out ) :: HeadLine  !---文件头的行数Integer , Intent( out ) :: Row !---文件行数RowInteger, Intent( out ) :: Column !---最大列数ColumnCharacter(Len = 1000) :: CLineInteger:: IOStatus=0Real,dimension(LEN(CLine)):: RealArrayInteger:: nsize=0!---------------------------------------------Row=0HeadLine=0Column=0!---------------------------------------------!---获取Row和Columnclose(9001)!---先测试出文件行数和数据的最大列数open(unit=9001,file=FileName,status='OLD')Read( 9001 ,'(A1000)',iostat=IOStatus) CLineDo While (IOStatus == 0 )Row = Row + 1!---------------------------------------------call StrToRealArray(CLine,RealArray,nsize)!---------------------------------------------if (nsize>0 .AND.Column<nsize) thenColumn=nsizeend if!---------------------------------------------Read( 9001 ,'(A1000)',iostat=IOStatus) CLine!---------------------------------------------End Doclose(9001)!---------------------------------------------close(9001)!---------------------------------------------!---测试文件头HeadLineopen(unit=9001,file=FileName,status='OLD')Read( 9001 ,'(A1000)',iostat=IOStatus) CLine!---------------------------------------------call StrToRealArray(CLine,RealArray,nsize)!---------------------------------------------Do While (IOStatus==0 .AND. nsize < Column )!---------------------------------------------HeadLine=HeadLine+1!---------------------------------------------Read( 9001 ,'(A1000)',iostat=IOStatus) CLine!---------------------------------------------call StrToRealArray(CLine,RealArray,nsize)!---------------------------------------------End Doclose(9001)!---------------------------------------------end subroutine GetFileRowColumn!!=============================================================subroutine LoadFromFile4(FileName,Array2D,Row,Column,Error)!------------------------------------------------------------!---获取文本文件FileName的数据!---Array2D::大小(Row,Column),存放数据的二维可分配内存大小的数组!---Row::文件行数,Column::文件数据列数!---Error==0::表示读取文本文件中的数据正确,否则有误!---吴徐平2013-07-22(wxp07@qq.com)!------------------------------------------------------------Implicit NoneCharacter(Len = *), Intent( IN ) :: FileNameInteger ,Intent( OUT ) ::Row,ColumnReal(kind=4),allocatable,Intent( OUT ) :: Array2D(:,:)Character(Len = 1000) :: CLineReal,dimension(LEN(CLine)):: RealArrayInteger,Intent( OUT ):: ErrorInteger  :: TotalRow,HeadLine,nsize,i,j,IOStatus!---------------------------------------Row=0Column=0TotalRow=0HeadLine=0nsize = 0i = 0j=0Error = 0IOStatus = 0RealArray=0.0!---------------------------------------call GetFileRowColumn(FileName,HeadLine,TotalRow,Column)Row=TotalRow-HeadLine !只包含数据的行数!---------------------------------------if (Row>0 .AND. Column>0 ) then!--------------------------if (Allocated(Array2D)) thendeallocate(Array2D)end ifallocate(Array2D(Row,Column),stat=Error)Array2D=0.0 !初始化为0!------------------------if (Error==0) then!--------------------------close(9002)!---------------------------------------open(unit=9002,file=FileName,status='OLD')!---------------------------------------do i=1,TotalRowRead( 9002 , '(A1000)' ,iostat=IOStatus) CLineif (IOStatus==0 .AND. i>HeadLine) then!---------------------------------------call StrToRealArray(CLine,RealArray,nsize)!---------------------------------------if (nsize >0) then!---------------------------------------do j=1,nsizeArray2D(i-HeadLine,j)=RealArray(j)end do!---------------------------------------end if!---------------------------------------end ifend do!---------------------------------------close(9002)!---------------------------------------elseError=-1 !分配内存失败end if!---------------------------------------elseError=-1 !文本文件中没有数据end if!---------------------------------------end subroutine LoadFromFile4!!=============================================================!=============================================================subroutine LoadFromFile8(FileName,Array2D,Row,Column,Error)!------------------------------------------------------------!---获取文本文件FileName的数据!---Array2D::大小(Row,Column),存放数据的二维可分配内存大小的数组!---Row::文件行数,Column::文件数据列数!---Error==0::表示读取文本文件中的数据正确,否则有误!---吴徐平2013-07-22(wxp07@qq.com)!------------------------------------------------------------Implicit NoneCharacter(Len = *), Intent( IN ) :: FileNameInteger ,Intent( OUT ) ::Row,ColumnReal(kind=8),allocatable,Intent( OUT ) :: Array2D(:,:)Character(Len = 1000) :: CLineReal,dimension(LEN(CLine)):: RealArrayInteger,Intent( OUT ):: ErrorInteger  :: TotalRow,HeadLine,nsize,i,j,IOStatus!---------------------------------------Row=0Column=0TotalRow=0HeadLine=0nsize = 0i = 0j=0Error = 0IOStatus = 0RealArray=0.0!---------------------------------------call GetFileRowColumn(FileName,HeadLine,TotalRow,Column)Row=TotalRow-HeadLine !只包含数据的行数!---------------------------------------if (Row>0 .AND. Column>0 ) then!--------------------------if (Allocated(Array2D)) thendeallocate(Array2D) !释放内存,重新分配end ifallocate(Array2D(Row,Column),stat=Error)Array2D=0.0 !初始化为0!------------------------if (Error==0) then!--------------------------close(9002)!---------------------------------------open(unit=9002,file=FileName,status='OLD')!---------------------------------------do i=1,TotalRowRead( 9002 , '(A1000)' ,iostat=IOStatus) CLineif (IOStatus==0 .AND. i>HeadLine) then!---------------------------------------call StrToRealArray(CLine,RealArray,nsize)!---------------------------------------if (nsize >0) then!---------------------------------------do j=1,nsizeArray2D(i-HeadLine,j)=RealArray(j)end do!---------------------------------------end if!---------------------------------------end ifend do!---------------------------------------close(9002)!---------------------------------------elseError=-1 !分配内存失败end if!---------------------------------------elseError=-1 !文本文件中没有数据end if!---------------------------------------end subroutine LoadFromFile8!!=============================================================end module StrNum


原创粉丝点击