gtool5 Fortran 90/95 Library 1.0.0-rc5
日本語
Loading...
Searching...
No Matches
dc_iounit::fileopen Interface Reference

Public Member Functions

subroutine fileopen (unit, file, mode, err)

Detailed Description

Definition at line 131 of file dc_iounit.f90.

Constructor & Destructor Documentation

◆ fileopen()

subroutine dc_iounit::fileopen::fileopen ( integer, intent(out) unit,
character(*), intent(in) file,
character(*), intent(in), optional mode,
logical, intent(out), optional err )

Open a file and return unit number

Filename is given to file, and open mode is given to mode, then the file is opened and unit number is returned.

Mode Description
"r" A file is opened with read-only mode
"w" A file is opened with writable mode. If a file is exist already, the content of the file is emptied.
"a" A file is opened with writable mode. Output is appended at the end of the file.
"rw" A file is opened with read/write mode. If a file is exist already, the content of the file is emptied.
"ra" A file is opened with read/write mode. If a file is exist already, a position of read/write is set at the end of the file.

If the file can not be opened with the mode, the program aborts. If this err argument is given, .true. is substituted to err and -1 is substituted to unit and the program does not abort.

Parameters
[out]unitUnit number assigned to the opened file
[in]fileFilename to open
[in]modeOpen mode ("r", "w", "a", "rw", "ra"). Default is "r".
[out]errException handling flag

Definition at line 186 of file dc_iounit.f90.

189 use dc_types, only: string, token
190 use dc_trace, only: beginsub, endsub
191 use dc_error, only: storeerror, dc_noerr, &
195 use dc_string, only: tochar, tolower
196 implicit none
197 integer, intent(out):: unit
198 character(*), intent(in):: file
199 character(*), intent(in), optional:: mode
200 logical, intent(out), optional:: err
201
202 !-----------------------------------
203 ! 作業変数
204 ! Work variables
205 integer, parameter:: max_unit = 99
206 ! NAMELIST ファイルをオープンするための
207 ! 装置番号の最大値. Fortran で使用可能な
208 ! 範囲 (0〜99) のうち,
209 ! 最大値が設定されている.
210 !
211 ! Maximum unit number for open of
212 ! NAMELIST file. An maximum
213 ! value within the bounds of available number
214 ! in Fortran (0 - 99) is specified.
215 integer, parameter:: min_unit = 0
216 ! NAMELIST ファイルをオープンするための
217 ! 装置番号の最小値. Fortran で使用可能な
218 ! 範囲 (0〜99) のうち,
219 ! 最小値が設定されている.
220 !
221 ! Minimum unit number for open of
222 ! NAMELIST file. An minimum
223 ! value within the bounds of available number
224 ! in Fortran (0 - 99) is specified.
225 character(TOKEN):: open_mode
226 integer:: unit_work
227 logical:: unit_exist_flag, unit_opend_flag
228 logical:: file_exist_flag
229 integer:: iostat
230 integer:: stat
231 character(STRING):: cause_c
232 character(*), parameter:: subname = 'FileOpen'
233 continue
234 call beginsub(subname, version)
235 stat = dc_noerr
236 cause_c = ''
237 unit = -1
238
239 !-----------------------------------------------------------------
240 ! オプショナル引数のチェック
241 ! Check optional arguments
242 !-----------------------------------------------------------------
243 if (present_and_not_empty(mode)) then
244 open_mode = mode
245 else
246 open_mode = 'r'
247 end if
248 call tolower(open_mode)
249
250 !-----------------------------------------------------------------
251 ! 引数の正当性のチェック
252 ! Validation of arguments
253 !-----------------------------------------------------------------
254 if ( trim(file) == '' ) then
255 stat = dc_efilenameempty
256 goto 999
257 end if
258
259 !----------------------------------------------------------------
260 ! 使用可能な装置番号の探査
261 ! Search available unit number
262 !----------------------------------------------------------------
263 unit_work = max_unit
264 do
265 inquire(unit=unit_work, exist=unit_exist_flag, opened=unit_opend_flag)
266 if (unit_exist_flag .and. .not. unit_opend_flag) then
267 exit
268 endif
269 unit_work = unit_work - 1
270 if (unit_work < min_unit) then
271 cause_c = tochar(min_unit) // ' - ' // tochar(max_unit)
272 stat = dc_enounitnum
273 goto 999
274 end if
275 enddo
276
277 !----------------------------------------------------------------
278 ! モードの書式のチェック
279 ! Check form of mode
280 !----------------------------------------------------------------
281 select case( trim(open_mode) )
282 case ('r', 'w', 'rw', 'a', 'ra')
283 case default
284 cause_c = open_mode
285 stat = dc_ebadfileopmode
286 goto 999
287 end select
288
289 !----------------------------------------------------------------
290 ! ファイルの存在のチェック
291 ! Check existance of a file
292 !----------------------------------------------------------------
293 select case( trim(open_mode) )
294 case ('r')
295 inquire(file=file, exist=file_exist_flag)
296 if (.not. file_exist_flag) then
297 cause_c = file
298 stat = dc_enofileexist
299 goto 999
300 end if
301 end select
302
303 !----------------------------------------------------------------
304 ! ファイルの読み込み可能のチェック
305 ! Check readable of a file
306 !----------------------------------------------------------------
307 select case( trim(open_mode) )
308 case ('r')
309 open(unit=unit_work, iostat=iostat, &
310 & file=file, status='OLD', action='READ')
311 if (.not. iostat == 0) then
312 cause_c = file
313 stat = dc_enofileread
314 goto 999
315 end if
316 close(unit=unit_work)
317 end select
318
319 !----------------------------------------------------------------
320 ! ファイルの書き込み可能のチェック
321 ! Check writable of a file
322 !----------------------------------------------------------------
323 select case( trim(open_mode) )
324 case ('w', 'a', 'rw', 'ra')
325 open(unit=unit_work, iostat=iostat, &
326 & file=file, status='UNKNOWN', action='WRITE')
327 if (.not. iostat == 0) then
328 cause_c = file
329 stat = dc_enofilewrite
330 goto 999
331 end if
332 close(unit=unit_work)
333 end select
334
335 !----------------------------------------------------------------
336 ! ファイルオープン
337 ! Open a file
338 !----------------------------------------------------------------
339 select case( trim(open_mode) )
340 case ('r')
341 open(unit=unit_work, file=file, &
342 & status='OLD', action='READ')
343
344 case ('w')
345 open(unit=unit_work, file=file, &
346 & status='REPLACE', action='WRITE')
347
348 case ('rw')
349 open(unit=unit_work, file=file, &
350 & status='REPLACE', action='READWRITE')
351
352 case ('a')
353 open(unit=unit_work, file=file, &
354 & status='UNKNOWN', position='APPEND', action='WRITE')
355
356 case ('ra')
357 open(unit=unit_work, file=file, &
358 & status='UNKNOWN', position='APPEND', action='READWRITE')
359
360 end select
361
362 unit = unit_work
363
364999 continue
365 call storeerror(stat, subname, err, cause_c)
366 call endsub(subname)
Error handling module.
Definition dc_error.f90:454
subroutine, public storeerror(number, where, err, cause_c, cause_i)
Definition dc_error.f90:891
integer, parameter, public dc_ebadfileopmode
Definition dc_error.f90:540
integer, parameter, public dc_noerr
Error storage variables
Definition dc_error.f90:468
integer, parameter, public dc_enofileread
Definition dc_error.f90:543
integer, parameter, public dc_efilenameempty
Definition dc_error.f90:539
integer, parameter, public dc_enofilewrite
Definition dc_error.f90:544
integer, parameter, public dc_enofileexist
Definition dc_error.f90:542
integer, parameter, public dc_enounitnum
Definition dc_error.f90:541
Judge optional control parameters.
logical function, public present_and_not_empty(arg)
Handling character types.
Definition dc_string.f90:83
Debug tracing module.
Definition dc_trace.f90:150
subroutine, public beginsub(name, fmt, i, r, d, l, n, c1, c2, c3, ca, version)
Definition dc_trace.f90:476
subroutine, public endsub(name, fmt, i, r, d, l, n, c1, c2, c3, ca)
Definition dc_trace.f90:599
Provides kind type parameter values.
Definition dc_types.f90:55
integer, parameter, public token
Character length for word, token
Definition dc_types.f90:128
integer, parameter, public string
Character length for string
Definition dc_types.f90:137

References dc_trace::beginsub(), dc_error::dc_ebadfileopmode, dc_error::dc_efilenameempty, dc_error::dc_enofileexist, dc_error::dc_enofileread, dc_error::dc_enofilewrite, dc_error::dc_enounitnum, dc_error::dc_noerr, dc_trace::endsub(), dc_present::present_and_not_empty(), dc_error::storeerror(), dc_types::string, and dc_types::token.

Here is the call graph for this function:

The documentation for this interface was generated from the following file: