Skip to content

Implement open(filename, mode) and use it #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Jan 4, 2020
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 43 additions & 7 deletions src/stdlib_experimental_io.f90
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
module stdlib_experimental_io
use iso_fortran_env, only: sp=>real32, dp=>real64, qp=>real128
use stdlib_experimental_error, only: error_stop
implicit none
private
public :: loadtxt, savetxt
public :: loadtxt, savetxt, open

interface loadtxt
module procedure sloadtxt
Expand Down Expand Up @@ -46,7 +47,7 @@ subroutine sloadtxt(filename, d)
integer :: s
integer :: nrow,ncol,i

open(newunit=s, file=filename, status="old", action="read")
s = open(filename)

! determine number of columns
ncol = number_of_columns(s)
Expand Down Expand Up @@ -89,7 +90,7 @@ subroutine dloadtxt(filename, d)
integer :: s
integer :: nrow,ncol,i

open(newunit=s, file=filename, status="old", action="read")
s = open(filename)

! determine number of columns
ncol = number_of_columns(s)
Expand Down Expand Up @@ -132,7 +133,7 @@ subroutine qloadtxt(filename, d)
integer :: s
integer :: nrow,ncol,i

open(newunit=s, file=filename, status="old", action="read")
s = open(filename)

! determine number of columns
ncol = number_of_columns(s)
Expand Down Expand Up @@ -164,7 +165,7 @@ subroutine ssavetxt(filename, d)
! call savetxt("log.txt", data)

integer :: s, i
open(newunit=s, file=filename, status="replace", action="write")
s = open(filename, "w")
do i = 1, size(d, 1)
write(s, *) d(i, :)
end do
Expand All @@ -187,7 +188,7 @@ subroutine dsavetxt(filename, d)
! call savetxt("log.txt", data)

integer :: s, i
open(newunit=s, file=filename, status="replace", action="write")
s = open(filename, "w")
do i = 1, size(d, 1)
write(s, *) d(i, :)
end do
Expand All @@ -210,7 +211,7 @@ subroutine qsavetxt(filename, d)
! call savetxt("log.txt", data)

integer :: s, i
open(newunit=s, file=filename, status="replace", action="write")
s = open(filename, "w")
do i = 1, size(d, 1)
write(s, *) d(i, :)
end do
Expand Down Expand Up @@ -268,4 +269,39 @@ logical function whitechar(char) ! white character
end if
end function

integer function open(filename, mode) result(u)
! Open a file
!
! To open a file to read:
!
! u = open("somefile.txt") # The default `mode` is "r"
! u = open("somefile.txt", "r")
!
! To open a file to write:
!
! u = open("somefile.txt", "w")

! To append to the end of the file if it exists:
!
! u = open("somefile.txt", "a")

character(*), intent(in) :: filename
character(*), intent(in), optional :: mode
character(:), allocatable :: mode_
mode_ = "r"
if (present(mode)) mode_ = mode
! Note: the Fortran standard says that the default values for `status` and
! `action` are processor dependent, so we have to explicitly set them below
if (mode_ == "r") then
open(newunit=u, file=filename, status="old", action="read")
else if (mode_ == "w") then
open(newunit=u, file=filename, status="replace", action="write")
else if (mode_ == "a") then
open(newunit=u, file=filename, position="append", status="old", &
action="write")
else
call error_stop("Unsupported mode")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there some rules for error messages? Usually, I would mention something a bit more verbose to help the user, e.g.

     call error_stop("ERROR, open function: Unsupported mode")

Maybe worthwhile to open an issue about that?

end if
end function

end module