Quantcast
Channel: Intel® Fortran Compiler for Linux* and macOS*
Viewing all articles
Browse latest Browse all 2583

integer to string folder and file creation

$
0
0

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

 


Viewing all articles
Browse latest Browse all 2583

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>