read_file_to_char_array Function

public function read_file_to_char_array(filename, border) result(array)

Read a file into a 2d character array.

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: filename
character(len=1), intent(in), optional :: border

if true, extra border is added with this char

Return Value character(len=1), dimension(:,:), allocatable


Calls

proc~~read_file_to_char_array~~CallsGraph proc~read_file_to_char_array aoc_utilities::read_file_to_char_array proc~number_of_lines_in_file aoc_utilities::number_of_lines_in_file proc~read_file_to_char_array->proc~number_of_lines_in_file proc~read_line aoc_utilities::read_line proc~read_file_to_char_array->proc~read_line

Called by

proc~~read_file_to_char_array~~CalledByGraph proc~read_file_to_char_array aoc_utilities::read_file_to_char_array proc~go~5 problem_23::go proc~go~5->proc~read_file_to_char_array proc~go~6 problem_11::go proc~go~6->proc~read_file_to_char_array program~problem_10 problem_10 program~problem_10->proc~read_file_to_char_array program~problem_16 problem_16 program~problem_16->proc~read_file_to_char_array program~problem_21 problem_21 program~problem_21->proc~read_file_to_char_array program~problem_3 problem_3 program~problem_3->proc~read_file_to_char_array program~problem_11 problem_11 program~problem_11->proc~go~6 program~problem_23 problem_23 program~problem_23->proc~go~5

Source Code

    function read_file_to_char_array(filename, border) result(array)
        character(len=*),intent(in) :: filename
        character(len=1),intent(in),optional :: border !! if true, extra border is added with this char
        character(len=1),dimension(:,:),allocatable :: array

        integer :: i, j, iunit, n_lines, n_cols
        character(len=:),allocatable :: line

        open(newunit=iunit, file=filename, status='OLD')
        n_lines = number_of_lines_in_file(iunit)
        line = read_line(iunit); n_cols = len(line)
        rewind(iunit)

        if (present(border)) then
            allocate(array(0:n_lines+1, 0:n_cols+1)) ! padding with border
            array = border
        else
            allocate(array(n_lines, n_cols))
        end if

        do i = 1, n_lines
            line = read_line(iunit)
            do j = 1, n_cols
                array(i,j) = line(j:j)
            end do
        end do
        close(iunit)

    end function read_file_to_char_array