Temporary Files And Directories in Fortran
Our program will demonstrate how to work with temporary files and directories in Fortran. Here’s the full source code:
program temp_files_and_dirs
use, intrinsic :: iso_fortran_env, only: error_unit
use, intrinsic :: iso_c_binding, only: c_char, c_null_char
implicit none
character(len=:), allocatable :: temp_file_name, temp_dir_name
integer :: unit_num, iostat
character(len=100) :: error_msg
! Create a temporary file
call create_temp_file(temp_file_name)
print *, "Temp file name:", temp_file_name
! Write some data to the file
open(newunit=unit_num, file=temp_file_name, status='replace', action='write', iostat=iostat, iomsg=error_msg)
if (iostat /= 0) then
write(error_unit,*) "Error opening file: ", trim(error_msg)
stop
end if
write(unit_num, '(4I1)') 1, 2, 3, 4
close(unit_num)
! Create a temporary directory
call create_temp_dir(temp_dir_name)
print *, "Temp dir name:", temp_dir_name
! Create a file in the temporary directory
call create_file_in_dir(temp_dir_name)
! Clean up
call delete_file(temp_file_name)
call delete_directory(temp_dir_name)
contains
subroutine create_temp_file(file_name)
character(len=:), allocatable, intent(out) :: file_name
character(len=15) :: template = "sampletempXXXXXX"
integer :: fd
allocate(character(len=256) :: file_name)
call mkstemp(template, fd, file_name)
close(fd)
end subroutine create_temp_file
subroutine create_temp_dir(dir_name)
character(len=:), allocatable, intent(out) :: dir_name
character(len=19) :: template = "sampledirtempXXXXXX"
allocate(character(len=256) :: dir_name)
call mkdtemp(template, dir_name)
end subroutine create_temp_dir
subroutine create_file_in_dir(dir_name)
character(len=*), intent(in) :: dir_name
character(len=:), allocatable :: full_path
integer :: unit_num, iostat
character(len=100) :: error_msg
full_path = trim(dir_name) // "/file1"
open(newunit=unit_num, file=full_path, status='replace', action='write', iostat=iostat, iomsg=error_msg)
if (iostat /= 0) then
write(error_unit,*) "Error creating file in directory: ", trim(error_msg)
return
end if
write(unit_num, '(2I1)') 1, 2
close(unit_num)
end subroutine create_file_in_dir
subroutine delete_file(file_name)
character(len=*), intent(in) :: file_name
logical :: exist
integer :: iostat
inquire(file=file_name, exist=exist)
if (exist) then
open(unit=10, file=file_name, status='old', iostat=iostat)
if (iostat == 0) close(10, status='delete')
end if
end subroutine delete_file
subroutine delete_directory(dir_name)
character(len=*), intent(in) :: dir_name
character(len=1024) :: command
write(command, '(3A)') 'rm -rf "', trim(dir_name), '"'
call execute_command_line(command)
end subroutine delete_directory
end program temp_files_and_dirs
This Fortran program demonstrates how to work with temporary files and directories. Here’s a breakdown of what it does:
We create a temporary file using the
create_temp_file
subroutine, which uses themkstemp
function to generate a unique filename.We write some data to the temporary file.
We create a temporary directory using the
create_temp_dir
subroutine, which uses themkdtemp
function to generate a unique directory name.We create a file inside the temporary directory.
Finally, we clean up by deleting both the temporary file and directory.
Note that Fortran doesn’t have built-in functions for creating temporary files and directories like some other languages do. We’ve used the POSIX functions mkstemp
and mkdtemp
, which may not be available on all systems. For a more portable solution, you might need to implement your own temporary file/directory creation logic.
To compile and run the program:
$ gfortran -o temp_files_and_dirs temp_files_and_dirs.f90
$ ./temp_files_and_dirs
Temp file name: /tmp/sampletemp123456
Temp dir name: /tmp/sampledirtemp789012
The exact output will vary as the filenames and directory names are generated randomly to ensure uniqueness.
Remember to handle errors appropriately in a production environment, and be aware of the security implications of creating and deleting files and directories.