Reputation: 5450
I want to read entries from a huge file (more than 10^10 rows) inside a loop. Since file is huge, I am reading one element at a time, using it and then reading the next element and so on. Thus, I have something like this:
DO i = 1, 10
open(unit = 1, file = 'myfile.dat')
Do j = 1, 10^10
read(1, *)x
!Use x here
Enddo
close(1)
ENDDO
This works fine when the outer loop runs a small number of times. However, when I want try to run it many more times, (say 100 to 1000), my computer gets hung or throws an error : Not enough space on \tmp
. I looked at this but that didn't solve my issue. My primary guess is that this is happening because each time I re-open the file, it gets stored in RAM or tmp but I am not sure about this. In this context, can anybody tell me a better way to load file only once but read the contents over and over ?
Upvotes: 0
Views: 1464
Reputation: 36
You don't have to close the file, just "rewind" it:
open(newunit = iunit, file = 'myfile.dat') !no need to specify the number, the compiler will do it for you, iunit is an integer
do i = 1, 10
do j = 1, 10**10 !fortran compilable
read(iunit, *)x
!Use x here
end do
rewind(iunit)
end do
close(iunit)
Upvotes: 2