Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Fortran, Using a loop and combined string to read a file

kkhhhkk

Coder
Hello, I'm trying to use f90 to read multiple files using do loop, here is what I have:

Code:
program rotate

    implicit none
    integer :: i
    character(len=20) :: MyStr, str1, str2, str_combined
    character(len=50) :: filename
    integer, parameter :: n = 10, m = 3
    real :: matrix(n,m)
    integer :: j, k
    character(len=200) :: line

    do i = 1,1368
        !everything inside
        str1 = 'coordinates_'
        str2 = '.ye'
        write(MyStr,'(i0)') i
        !above turns integer to string

        filename = "(A)",trim(str1)//trim(adjustl(MyStr))//trim(str2)
        open(10,file="filename",status='old')
        do j=1,n
          read(10,'(A)') line
          read(line, *)matrix(i,:)
        enddo
        close(10)

        do j=1,n
        write(*, '(3F10.6)') matrix(i,:)
        enddo


    end do

end program rotate

This script opens 1368 files and reads the content as a matrix, however I get an error when it tries to read the filename, how can can I fix this?
 
Wow, Fortran.. that's been a while for me. I'm a bit confused by you writing I get an error when it tries to read the filename. I guess you mean you get an error when it tries to open (and/or read) the file ?
open(10,file="filename",status='old')
Should you not be using the value of the variable filename, which you had just constructed, rather than the literal string "filename" ?
 
Wow, Fortran.. that's been a while for me. I'm a bit confused by you writing I get an error when it tries to read the filename. I guess you mean you get an error when it tries to open (and/or read) the file ?
open(10,file="filename",status='old')
Should you not be using the value of the variable filename, which you had just constructed, rather than the literal string "filename" ?
So I tried that, also dropped the "(A)", and I end up with the error:

rotate.f90:19:8:

filename = "(A)",trim(str1)//trim(adjustl(MyStr))//trim(str2)
1
Error: Unclassifiable statement at (1)
 
Alright update: The following script is able to read all the file and print its contents:
Code:
program rotate

    implicit none
    integer :: i
    integer :: unit
    character(len=50) :: filename
    integer, parameter :: n = 10, m = 3
    real :: matrix(n,m)
    integer :: j, k
    character(len=256) :: line
    integer :: io

    do i = 1,1368
        !everything inside

        write(filename, '(a, i0, a)') 'coordinates_', i, '.ye'
        open(newunit=unit,file=filename,status='old',action='read')
        print *, trim(filename)
        do while (.true.)
          read(unit,'(a)',iostat=io) line
          if (io < 0) exit
          print *, line
        end do
        close(unit)



    end do

end program rotate

I'm now trying to figure out how to take the values starting from the second index of each line (the first indexed character is not a number) and I want to read it as a matrix that I can do maths stuff with
 
Sorry I can't help you with that. My last exposure to Fortran was almost 50 years ago I think, which must have been Fortran 66. Seems there's a lot of newer stuff since the nineties.
Just curious why you are using this old language ? Does it offer you something unique you need for this project but can't find in any modern language ?
 
Sorry I can't help you with that. My last exposure to Fortran was almost 50 years ago I think, which must have been Fortran 66. Seems there's a lot of newer stuff since the nineties.
Just curious why you are using this old language ? Does it offer you something unique you need for this project but can't find in any modern language ?
Yeah I'm a student doing honours in physics, I need to use fortran for my specific project (molecule rotations and calculations). I'm sure what I need to do can be done in c++ (I dont know how to use c++) however my supervisor told me to use fortran cause she said she could check over it (but she hasn't been able to)
 
Yeah I'm a student doing honours in physics, I need to use fortran for my specific project (molecule rotations and calculations). I'm sure what I need to do can be done in c++ (I dont know how to use c++) however my supervisor told me to use fortran cause she said she could check over it (but she hasn't been able to)
Once upon a time, Fortran had the edge over competitors (which I guess were mainly Cobol and perhaps Algol) for its offering of computational functions. These days, that is most probably not the case anymore, so I find it quite peculiar that a modern tutor/supervisor would still recommend Fortran. Perhaps she's from an older generation and never adapted to newer languages ? She must realize it's not easy to get help with it.
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom