I made some dummy code to learn how to open and read a file. Let's say I have the following test.dat that reads
1
2
3
4
5
6
7
8
9
10
I wrote the following code to open and read the data file
subroutine readdata
implicit none
integer :: j
double precision :: test
open(unit = 100, file = 'test.dat', status = 'old', action = 'read')
do j = 1, 10
read(100,*) test
print *, 'N1=', test
end do
end subroutine
The result is shown below as expected.
gfortran -g -I/usr/include -o main main.o subroutines.o -L/usr/lib64/liblapack -L/usr/lib64/libblas
test= 1.0000000000000000
test= 2.0000000000000000
test= 3.0000000000000000
test= 4.0000000000000000
test= 5.0000000000000000
test= 6.0000000000000000
test= 7.0000000000000000
test= 8.0000000000000000
test= 9.0000000000000000
test= 10.000000000000000
Main finished.
However, if the data is stored on one line as follows
1 2 3 4 5 6 7 8 9 10
then the above code does not work as desired. It only reads the first element in the line and then gives an error message
sharwani@linux-h6qd:~/PHD_research/myCodes/data> ./runcase.sh
rm -f *.o *.mod *.MOD *.exe *.stackdump main
gfortran -g -I/usr/include -c main.f90
gfortran -g -I/usr/include -c subroutines.f90
gfortran -g -I/usr/include -o main main.o subroutines.o -L/usr/lib64/liblapack -L/usr/lib64/libblas
test= 1.0000000000000000
At line 9 of file subroutines.f90 (unit = 100, file = 'test.dat')
Fortran runtime error: End of file
So my question is that I have a data file that contains 2879 (1 x 2879) numbers stored on one line. How will I open and read all these numbers in a data file?
source
share