Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28988 Discussions

integer to string folder and file creation

conor_p_
Beginner
855 Views

Hello everyone, I have the following code which is supposed to convert an integer i to a string, create a folder called i, and then covert a double Oh to stiring, and make a file called Oh. I can get this to work using gfortran, however I am having issues using IFORT. From what I gathered on these forum is that the appropriate intel call system(mkdir) equivalent is MakeDirQQ, however it appears I am not using it correctly. Does anyone see what I am doing wrong here. I am getting a no such file or directory error.

!-*- mode: f90;-*-  
Program Main
  implicit none
  integer, parameter :: rk = selected_real_kind(p=15), ik = 4
  logical :: e,result
  integer :: i
  double precision :: Oh
  character(LEN=150) :: folder  !Make sure these are long enough
  character(LEN=40) :: file
  
  !Shows how to create file names and folders automatically based on values
  !stored in variables

  !First we want to create a numbered folder with the same value as i
  i = 8
  
  !This prints the value of i to the string folder with format i8
  write(folder,'(i8)') i

  !Removes leading whitespace, always use this after printing value into string
  folder = ADJUSTL(folder)  

  !This adds the slash for the end of the folder, trim removes empty space at the end
  folder = trim(folder)//'/'  
  
  !INQUIRE will check if this folder already exists, if not it will create it
  INQUIRE(FILE=trim(folder),EXIST=e)
  if (.not.e) then
     !call system('mkdir '//trim(folder))            !system issues terminal commands, in this case creates directory
     result = MakeDirQQ('i')
     write(*,*) 'Created directory '//trim(folder)
  else
     write(*,*) trim(folder)//' already exists'
     
  end if
  


  !Now say we want to create a file in the folder we just created
  Oh = 0.5
  write(file,'(f5.3)') Oh    !Write value of Oh to file
  file = ADJUSTL(file)       !Remove leading whitespace
  file = trim(file)//'.dat'  !Append file extension
  
  Open(unit = 11,file = trim(folder)//trim(file), status = 'replace')
  write(11,'(A)') 'Test'
  close(11)

  !Notice how I always use trim when using strings
  return

  

      
end Program Main

 

0 Kudos
2 Replies
Izaak_Beekman
New Contributor II
855 Views

I have never used MakeDirQQ() before but I’m guessing your issue is on line 30. Here it looks like you are creating a folder called ‘i’ rather than the folder name you have constructed. Replace ‘i’ with trim(folder) as the argument to MakeDirQQ.

0 Kudos
conor_p_
Beginner
855 Views

That worked! thank you!

0 Kudos
Reply