fortran,fortran90,multiple-files , How to open and read multiple files in Fortran 90


How to open and read multiple files in Fortran 90

Question:

Tag: fortran,fortran90,multiple-files

I have some issues about opening and reading multiple files. I have to write a code which reads two columns in n files formatted in the same way (they are different only for the values...). Before this, I open another input file and an output file in which I will write my results. I read other questions in this forum (such as this one) and tried to do the same thing, but I receive these errors:

 read(fileinp,'(I5)') i-49
                   1
 devstan.f90:20.24:

 fileLoop : do i = 50,52
                    2
 Error: Variable 'i' at (1) cannot be redefined inside loop beginning at (2)

and

 read(fileinp,'(I5)') i-49
           1
 Error: Invalid character in name at (1)

My files are numbered from 1 to n and are named 'lin*27-n.dat' (where n is the index starts from 1) and the code is:

 program deviation
  implicit none

  character(len=15) :: filein,fileout,fileinp
  integer :: row,i,h
  real :: usv,usf,tsv,tsf,diff


  write(*,'(2x,''Input file .......''/)')
  read(*,'(a12)') filein
  write(*,'(2x,''Output file........''/)')
  read(*,'(a12)') fileout
  open(unit = 30,File=filein)
  open(unit = 20,File=fileout)


  fileLoop : do i = 50,52
  fileinp = 'lin*27-'
  read(fileinp,'(I5)') i-49
  open(unit = i,File=fileinp)


   do row = 1,24
    read(30,*) h,usv,tsv
    read(i,*)  h,usf,tsf

     diff = usf - usv

    write(20,*) diff
   enddo
   close(i)
  enddo fileLoop

 end program deviation

How can I solve it? I am not pro in Fortran, so please don't use difficult language, thanks.


Answer:

The troublesome line is

read(fileinp,'(I5)') i-49

You surely mean to do a write (as in the example linked): this read statement attempts to read from the variable fileinp rather than writing to it.

That said, simply replacing with write is probably not what you need either. This will ignore the previous line

fileinp = 'lin*27-'

merely setting to, in turn, "1", "2", "3" (with leading blanks). Something like (assuming you intend that * to be there)

write(fileinp, '("lin*27-",I1)') i-49

Note also the use of I1 in the format, rather than I5: one may want to avoid blanks in the filename. [This is suitable when there is exactly one digit; look up Iw.m and I0 when generalizing.]


Related:


Fortran derived type in common: initialization?


fortran,fortran90,static-initialization
I got troubles with this common: COMMON /REDCOM/ DPREC,NITMA,INDIC,NBERR,NCAR,KMOTLU, & REDVAR,MOCDER(2) COMMON /REDCO1/ CTEXT C type(double_st) :: DPREC INTEGER :: NITMA,INDIC,NBERR,NCAR,KMOTLU,REDVAR,MOCDER CHARACTER(72) :: CTEXT CHARACTER(4) :: CTEXT4 C EQUIVALENCE (CTEXT,CTEXT4) The double_st derived type is: type double_st sequence real(kind(0.d0)) :: x,y,z integer :: acc = -1 end type double_st Trying...

Fortran compiler for mac to read program


compiler-errors,fortran,fortran77,g77
I'm using the g77 compiler (on terminal I just type g77 filename.for ) on a Mac. It can't read my fortran program and I can't modify the program (very old program, not mine, and I really just need the output from it). Any suggestion for another compiler to read the...

Ambiguous reference to variable


gcc,fortran,fortran90,gfortran,intel-fortran
So I am doing 2 modules which are linking to the main program. The first one has all the variables defined in it and the second one is with the functions. Module1: module zmienne implicit none integer, parameter :: ngauss = 8 integer, parameter :: out_unit=1000 integer, parameter :: out_unit1=1001...

Fortran memory allocation does not give an error, but the program is killed by OS at initialization


memory-management,fortran
Given the minimal working example provided below, do you know why the memory allocation error does not occur at memory allocation step? As I checked, when I use valgrind to run the code, or add parameter source=0.0 to memory allocation statement, then I have, as expected, the memory allocation error....

Setting size() of a scalar


fortran
In some legacy code, I found the following line size(k)=N What (if anything) does this do? As far as I know, it does not make sense to set size(thing) to a value in Fortran. Furthermore, k is implicitly defined as a scalar integer (i.e. there it is not declared in...

How can I implement BCD in Fortran?


binary,fortran,bcd
Not sure if the title of my question makes sense, so bear with me. I'd like to find a system for representing single digit numbers with as few bits as possible. There is a method called "Densely packed decimal" (https://en.wikipedia.org/wiki/Densely_packed_decimal) which would be my ideal solution, but I wouldn't even...

Precision not respected


visual-studio-2010,fortran
I use Visual Studio (2010 SP1) with Fortran IMSL (2011) and I can't get the right precision for my reals: program prova use, intrinsic :: iso_fortran_env implicit none integer, parameter :: ikind=selected_real_kind(p=8, r=99) real(kind=ikind) :: a=0.79 real(real64) :: b=0.79 real(kind=16) :: c=0.79 real(8) :: d=0.79 print *, a print *,...

gfortran doesn't compile real variables


fortran,gfortran
I have written a simple program in Fortran90 to calculate the area of a triangle. The user enters the three sides of the triangle, and then the program outputs the area. Simple enough. MODULE Triangle_Operations IMPLICIT NONE CONTAINS FUNCTION Area(x,y,z) REAL :: Area ! function type REAL, INTENT( IN )...

Fortran READ(*,*), WRITE(*,*) arguments


fortran
This question has been covered somewhat in previous SO questions. However, previous discussions seem somewhat incomplete. Fortran has several I/O statements. There is READ(*,*) and WRITE(*,*), etc. The first asterisk (*) is the standard asterisk designating an input or output from the keyboard to/from the screen. My question is about...

Fortran runtime error “bad integer for item 11 in list input”


io,fortran
I receive the runtime error "bad integer for item 11 in list input" referencing the following line of code. read(2,*)a,b,c,d,e,f,g,h,theta1,phi1,k,l,m,n,o, $ p,theta2,phi2,s,theta3,phi3, $ r1,x1,y1,r2,x2,y2,r3,x3,y3,z1,z2,z3 The line its reading from is 1 255.11211 0.2876 165.11404 90 4 8 0.19173 90 165.11404 0.09587 90 345.11404 4 4 0.0764 89.99915 -64.51149 0.11131 90.0015...

Unclassifiable statement at (1) in Fortran


gcc,fortran,fortran90,gfortran
I am pretty new to to fortran and I don't really know why am I getting this error. integrand(i)=inte(x(i),beta,r2,r1) 1 Error: Unclassifiable statement at (1) calka11.f95:97.6: I have made all the variables into a module file and then call them using use and when I am getting those variables into...

How do I extract a floating number in the middle of an alphanumeric chain?


fortran,fortran95
I am coding in Fortran 95. I must extract the two floating values from this line in an ascii file: circle(4097.0438,4111.337) Of course the READ statement is read(unit=11, fmt="(tr7,f9.4,tr1,f8.3)") x, y The problem is that I must do that for hundreds of ascii files, but there are variations in the...

Data exchange - Python and Fortran


python-2.7,fortran
We are developing a scientific application which has the interface in python 2.7 and the computation routines written in Intel Visual Fortran. Reading the source files is done using python, then only the required data for computations has to be passed to standalone Fortran algorithms. Once the computations done, the...

Using move_alloc in a derived type


fortran,alloc,derived-types
I have written a derived type that stores a multidimensional array. The array will be read from a file to an array b. I then want to transfer b to the derived type Space using member a. The advantage for mvalloc is not having to allocate two large arrays which...

How to make a random function in fortran to generate the same random distribution into array?


random,fortran,fortran90
I think my code below it's not exactly give me the same random distribution. subroutine trig_random_value() implicit none integer :: t, z, y, x real(real64) :: theta, r real(real64), parameter :: PI=4.D0*DATAN(1.D0) integer, dimension(12) :: date_time integer, dimension(12) :: seed call date_and_time(values=date_time) call random_seed seed = date_time(6) * date_time(7) +...

How to create a new directory from Force Fortran 2.0


directory,fortran,create-directory
I need to create a new directory from my code to be able to write a data file to it. I am using Force Fortran 2.0 from Windows 8 and I am also wondering if this syntax is going to vary from one operating system to the other due to...

How exactly do pointers in Fortran differ from C/C++ pointers?


c++,c,pointers,fortran
It might be useful to have both C/C++ programmers and Fortran programmers compare and contrast pointers in these two languages. In trying to explain to C/C++ programmers how Fortran pointers differ, I usually say pointers to functions or subroutines do not exist. I then try to make the argument that...

Using allocatable/assumed-size arrays with namelist read write


io,fortran,intel-fortran,fortran2003
I am using VS2012 and Intel Visual Fortran 2015. According to https://software.intel.com/en-us/forums/topic/269585, it is now allowed to use allocatable and assumed-size arrays with namelist read and write; however, I am still getting the error "A namelist-group-object must not be an assumed-size array". example code: subroutine writeGrid(fname, grid) character*(*) :: fname...

How to set a mpreal parameter inside a Fortran module [duplicate]


module,fortran
This question already has an answer here: Fortran derived types: Overloaded assignment operator not working with 'PARAMETER' attribute 2 answers I'd really like to be able to define some parameters in a Fortran module, so so that they are accessible for the main program and all its subroutines. The...

Using BLAS ?gemm on a subset of an array in fortran


arrays,fortran,slice,blas
The various LDx arguments to BLAS ?gemm functions are there to make it possible to operate on slices of larger arrays. For example, this small C program does a matrix multiplication of the top left and top right (100,100) submatrices of a (200,200) matrix, and stores the result in the...

Disabling fp-model strict when using ifort with f2py


python,numpy,optimization,fortran,f2py
I'm using f2py to offload performance critical sections of my python code to fortran. I have two fortran compilers, gfortran and ifort. Since the code I run will be on intel processors, ifort is usually faster. But for the most critical part of my code, I observe the opposite, with...

gfortran does not find symbol fabsq_ in libquadmath


fortran,precision,gfortran,quad
I'm trying to get quad precision to work with my FORTRAN code. I have to replace some intrinsic functions by those in libquadmath, i.e. the dabs function by fabsq. Unfortunately, if I compile the following test function program test integer dp parameter (dp=10) real(kind=dp) a a= -5.0_dp a=fabsq(a) write(*,*) "abs(a)",...

error: 'x' argument pf 'dtime' intrinsic at <1> must be of kind 4


fortran,gfortran
My understanding of programming is very limited so I hope I am making sense. I made a change to a fixed variable in a program (the program is called NAFnoise; I was using the .exe but it came with the source code and I made the change there). The program...

FORTRAN unformatted file write by each process


fortran,mpi,binaryfiles
In my parallel program, there was a big matrix. Each process computed and stored a part of it. Then the program wrote the matrix to a file by letting each process wrote its own part of the matrix in the correct order. The output file is in "unformatted" form. But...

Assigning values of an array in a loop


arrays,loops,fortran,fortran90,gfortran
In my code: DO i=1,numJog,1 IF(val(i) .EQV. .TRUE.)THEN DO j=1,contVenc,1 result(j) = i END DO END IF END DO Where val is a logical array, and result is a integer array. For example, if val is: F T F T Then, i=2 and i=4. But the result array just write...

Segmentation fault; core dumped Fortran


fortran,gfortran,fortran77
I would like to ask about the error occurred with my Fortran code. Since I'm new to fortran I can't handle this after 2 days, I also searched around but still don't know how to fix it. PROGRAM SUBDEM IMPLICIT REAL*8(A-H,O-Z) REAL*8 NTET,NPTK INTEGER*4 NA,NC,NE,NBAND,NTMAX,IDEF C CALL SETK08(NA,NC, A,C, PTK,NPTK,...

How can i store 2 numbers in a 1 byte char?


c++,c,binary,fortran,bcd
I have the question of the title, but If not, how could I get away with using only 4 bits to represent an integer? EDIT really my question is how. I am aware that there are 1 byte data structures in a language like c, but how could I use...

Calling C function with **int parameter from Fortran


c,fortran,fortran-iso-c-binding
Suppose that I have a C function with the following API : int c_function(int **a); How should I go about declaring a Fortran array/pointer to array, and passing it to the function, assuming that the C function is responsible for memory allocation ?...

ieee_arithmetic intrinsic module in gfortran


fortran,gfortran
I need to use gfortran to compile a library that is dependent on ieee_arithmetic. However, it is found that gfortran can not identify this module. For example with the code a.f90 program test use,intrinsic :: ieee_arithmetic real :: x read *, x if (ieee_is_nan(x)) then print *, "Nan" else print...

Filling Multidimensional Arrays


fortran,derived-types
I have created a derived type to access multi-dimensional arrays. With each array I associate a name in the array nm. My problem consists how to fill the array values once I have allocated the memory. An initial idea has been to use a multi-dimensional array as input. However I...

Fortran Seg Fault when assigning Matrices


matrix,segmentation-fault,fortran,derived-types
[UPDATE] the code and a few sentences were changed to reflect a realization explained in my second comment. The code should compile with the line below, however, I have an older gfortran and may not be seeing some errors that you might. gfortran BLU_implementation_copy.f90 -o BLU_implementation_copy.x I'm getting an incredibly...

Are the LAPACK routines thread safe?


multithreading,fortran,lapack,blas
I am a novice using the LAPACK routines, so I don't deeply know them, and I want to use them in parallelized loops (openmp). I use Ubuntu 14.04LTS and have LAPACK installed using my package manager. The version installed is: liblapack3 3.5.0-2ubuntu1 Library of linear algebra routines 3 - shared...

Accessing open files globally in Fortran


file-io,fortran,gfortran
Is there any means to accessing (reading, writing to) files that are opened in some other source code by just passing the unit number?

Compile Fortran source with Accelerate Framework (LAPACK and BLAS) [duplicate]


osx,makefile,frameworks,fortran
This question already has an answer here: How to use LDFLAGS in makefile 2 answers I wish to compile Fortran source code which uses functions from LAPACK and BLAS. When I compile a single source code file e.g. gfortran -g -framework accelerate test.f it works. However, I have many...

Fortran: makefile error


makefile,fortran,fortran90
I have a fortran main program called solidsolver.f90, and a module called read_mesh.f90. The module contains two subroutines and is used in the main program. I can compile them manually but not with a makefile. My makefile is named makefile.makefile, and it gives me an error: make: *** No targets...

Can I declare only specific elements of an array as constant? (C/C++/Fortran)


c++,c,arrays,fortran,constants
Scenario: I pass e.g. a 2D matrix as argument in a function and I would like to be able to read and change all the elements, BUT I would like to prevent any accidental writting in let's say the last column. So I would like somehow to tell the compiler...

unformatted output with wild card in Fortran


fortran,wildcard,fortran90
I've searched for a long time before I ask: I need to output a lot of unformatted files in Fortran to Ensight. I want to name them with geo.000000, geo.000001 ... geo.0001000. Here is how I deal with wild card: character(54) :: filename, temp character(80) :: buffer write(temp,'(i6.6)') step filename...

Checking all elements of array for a logical condition in fortran


gcc,fortran,gfortran
I want to check all rows of an array for a logical condition. I used function ALL as described in GNU GCC guide https://gcc.gnu.org/onlinedocs/gfortran/ALL.html Here is a sample code: program test3 implicit none real, allocatable, dimension (:,:) :: mat1 integer :: i,j,k,r logical :: lg r=3 allocate(mat1(r,r)) mat1=transpose( reshape( (/-1,-2,-3,-4,-5,-6,-7,-8,-9/),...

Batch file to conditionally execute ifortvars.bat


windows,batch-file,cmd,fortran
I am using a make.bat file to compile my Fortran code. To compile using the ifort command, I first need to initialize the compiler by executing ifortvarsbat intel64 command where I had to setup the path variable. This works fine, however, the path keeps increasing in size and says command...

Fortran DO loop, warning to use integer only


fortran,fortran90,gfortran,fortran95
I installed gfortran on my Ubuntu 15.04. While compiling in Fortran, be it .f95 or .f90 type, the DO loop asks to take integer parameters only but not real values or variables. That includes the loop variable and the step expression. Why can't it take real values too? The following...

Using Floor and Int in a computation


int,fortran,floor
I am using a calculation to locate a base pixel. Seems to me that the two calculations for i and j are equivalent (i.e. i==j is always true) when I tried a few examples. Is this always true? i = (int ((x - xmin) / a)) + 1 j =...

How to call Fortran routine with unit number argument from C


c,io,fortran,shared-libraries,abi
If I have a Fortran subroutine which takes a Fortran IO Unit as one of its parameters (for printing debug information to), and this function is compiled into a shared library, how do I correctly call this function from C? ! An example subroutine that I want to call from...