#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2006
    Posts
    163
    Rep Power
    27

    Concatenate integer with a character data type


    Is there any way in Fortran77 to concatenate an integer with a character data type?
    Basically, I'm trying to create filenames in a loop like:
    file01.out
    file02.out
    file03.out
    ....
    .......
    for this I need to concatenate 'file' which is a character with 01 which is an integer, and I cannot simply do file//01, since the data types are different.
    Is there a way to get around this issue in Fortran77.
    is there any function similar to sprintf in F77.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    394
    Rep Power
    52
    Hi!

    fortran Code:
    program concat
    implicit none
     
    integer intvar, i
    character string*2
     
    intvar = 10
     
    do i=1, 10
      write(unit=string, fmt='(I2)') intvar
      write(*,*) "file" // string // ".out"
      intvar = intvar + 1
    end do
     
    end program concat

    I leave the numbers 01 - 09 as an exercise to you (to be honest I have no idea how to do that )

    Regards, mawe
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2007
    Posts
    1
    Rep Power
    0

    Re:Concatenate integer with a character data type


    Hi,

    I hope it might work.

    PROGRAM concatinating_int_char

    IMPLICIT NONE
    CHARACTER(LEN = 20 ) :: file_name
    CHARACTER(LEN = 20 ) :: file_char
    INTEGER :: i

    file_char = '010203040506070809'
    DO i = 1,18,2
    file_name = 'output'// file_char(i:i+1)//'.dat'
    WRITE(*,*) file_name
    END DO

    END PROGRAM concatinating_int_char

    Enjoy,
    Rahu.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Posts
    1
    Rep Power
    0

    Read Files with diferente end names


    Next, the solution for you

    First yo need create three files: data1.txt, data2.txt, data3.txt.
    Each file have one integer number, i.e: 1 ,2, 3. The content of each file is saved in V1(. Enjoy it


    subroutine readata
    implicit none

    integer :: i,n,j
    CHARACTER (LEN=4) :: fname1,fname3
    CHARACTER (LEN=9) :: fullname
    CHARACTER (LEN=1) :: num
    integer, allocatable::V1(
    fname1='data' !nome principal do arquivo
    fname3='.txt' !tipo de arquivo que vai ser lido

    n=3 !numero de arquivos para ler
    allocate (V1(n)) !variavel onde vai ser guardado os dados do arquivo lido
    V1=0
    do j=1,n
    write(num,'(i1)') j !numeracao do arquivo
    fullname=fname1//num//fname3 !gera nome completo do arquivo
    open(unit=1,file=fullname,STATUS="OLD", ACTION="READ")
    read(unit=1,fmt=300) V1(j)
    close(unit=1)
    enddo

    300 format((i2))
    end subroutine readata

IMN logo majestic logo threadwatch logo seochat tools logo