Professional Documents
Culture Documents
If you want to determine the number of line in a given file within your FORTRAN code,
you can do this following nice trick!
program count_lines
implicit none
integer:: n
character(len=60):: cmd
cmd = "cat file_name.dat | grep '[^ ]' | wc -l > nlines.txt"
call system(cmd)
open(1,file='nlines.txt')
read(1,*) n
print*, "Number of lines are", n
cmd = 'rm nlines.txt'
call system(cmd)
end program
Thissimplecode,useslinuxcommandtofindthenumberoflines.
Noticethatcat file_name.dat | grep '[^ ]' | wc -l returnsthenumberoflinesina
filewithignotingtheblanklines.
inquire(file='file_name', exist=lexist)
where lexist is a logical variable. if it is .true. then the file is exisiting in the current directory!
We can also check whether a file has been opened in the code or not.
inquire(unit=11, opened=lopen)
or
inquire(file='file_name', opened=lopen)
if the file has been already opened, the logical variable lopen is .true. otherwise it is .false. .
Posted by Amin at No comments:
Email ThisBlogThis!Share to TwitterShare to FacebookShare to Pinterest
Object 1
Labels: Fortran