You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
`filename`: Shall be a character expression containing the file name.
29
+
`filename`: Shall be a `character(len=*)` scalar.
30
+
This argument is `intent(in)`.
31
+
Contains the file name.
25
32
26
-
`mode` (optional): Shall be a character expression containing characters describing the way in which the file will be used. The available modes are:
33
+
`mode`: Shall be a `character(len=*)` scalar.
34
+
This argument is `intent(in)` and `optional`.
35
+
Contains characters describing the way in which the file will be used (see [stdlib_io:open](https://stdlib.fortran-lang.org/page/specs/stdlib_io.html#description_1)). The available modes are:
27
36
28
37
| Character | Meaning |
29
38
| --------- | ------- |
@@ -38,40 +47,221 @@ Experimental
38
47
39
48
The default `mode` is `'rt'` (i.e. construct a file for reading a text file). The `mode` may include one of the four different methods for opening a file (i.e., `'r'`, `'w'`, `'x'`, and `'a'`). These four methods can be associated with the character `'+'` to open the file for updating. In addition, it can be specified if the file should be handled as a binary file (`'b'`) or a text file (`'t'`).
40
49
41
-
`ofile`: a file entity of `file` type.
42
-
43
-
### Return value
50
+
#### Return value
44
51
45
52
Returns a file entity of `file` type, in which the following are defined:
46
53
47
54
```fortran
48
55
type file
49
-
integer :: unit
50
56
character(:), allocatable :: filename
51
57
character(3) :: mode
58
+
integer :: unit
52
59
integer :: lines
53
60
contains
54
-
procedure :: exist => file_exist1
55
-
procedure :: open => open_file
56
-
procedure :: countlines => countlines1
61
+
procedure :: exist
62
+
procedure :: open
63
+
procedure :: countlines
57
64
procedure :: close
58
65
end type file
59
66
```
60
67
61
-
###Example
68
+
#### Examples
62
69
63
70
```fortran
64
-
program test_io_file
71
+
program demo_io_file_1
65
72
use forlab_io, only: file, disp
66
-
use stdlib_error, only: error_stop
73
+
use stdlib_error, only: check
67
74
type(file) :: infile
68
75
69
76
infile = file('DP.txt', 'r')
70
-
if(.not.infile%exist()) call error_stop('Error: File not exist, '//infile%filename)
77
+
call check(infile%exist(), msg="File not exist, "//infile%filename)
78
+
call infile%open()
79
+
call infile%countlines()
80
+
call disp(infile%lines, 'Linenumber in file is: ')
81
+
call infile%close() !! `infile` is closed, `infile%filename` is deallocated.
82
+
83
+
end program demo_io_file_1
84
+
```
85
+
86
+
```fortran
87
+
program demo_io_file_2
88
+
use forlab_io, only: file, disp
89
+
use stdlib_error, only: check
90
+
type(file), allocatable :: infile
91
+
92
+
!! To create a `file` type scalar to read:
93
+
!!
94
+
!!```fortran
95
+
!! infile = file("somefile.txt") ! The default `mode` is "rt"
96
+
!! infile = file("somefile.txt", "r")
97
+
!!```
98
+
!!
99
+
!! To create a `file` type scalar to write:
100
+
!! infile = file("somefile.txt", "w")
101
+
!!
102
+
!! To append to the end of the file if it exists:
103
+
!!
104
+
!! infile = file("somefile.txt", "a")
105
+
106
+
infile = file('DP.txt', 'r')
107
+
call check(infile%exist(), msg="File not exist, "//infile%filename)
71
108
call infile%open()
72
109
call infile%countlines()
73
110
call disp(infile%lines, 'Linenumber in file is: ')
74
-
call infile%close()
111
+
call infile%close() !! It is optional. `infile` is closed, `infile%filename` is deallocated.
112
+
deallocate(infile) !! `infile` is deallocated.
113
+
114
+
end program demo_io_file_2
115
+
```
116
+
117
+
### `file%exist`
118
+
119
+
#### Description
120
+
121
+
Checks the `file` is exist or not.
122
+
123
+
#### Status
124
+
125
+
Experimental.
126
+
127
+
#### Class
128
+
129
+
Impure function.
130
+
131
+
#### Syntax
132
+
133
+
`result = self % [[file(type):exist(bound)]]()`
134
+
135
+
#### Arguments
136
+
137
+
None.
138
+
139
+
#### Return value
140
+
141
+
Returns a `logical` scalar: the file exists as `.true.`, the file does not exist as `.false.`.
142
+
143
+
#### Example
144
+
145
+
```fortran
146
+
program demo_io_file_exist
147
+
use forlab_io, only: file
148
+
use stdlib_error, only: check
149
+
type(file), allocatable :: infile
150
+
151
+
infile = file("filename.txt", "r")
152
+
call check(infile%exist(), msg="File not exist: "//infile%filename)
153
+
deallocate(infile)
154
+
155
+
end program demo_io_file_exist
156
+
```
157
+
158
+
### `file%open`
159
+
160
+
#### Description
161
+
162
+
Open the `file` object.
163
+
164
+
#### Status
165
+
166
+
Experimental.
167
+
168
+
#### Class
169
+
170
+
Impure function.
171
+
172
+
#### Syntax
173
+
174
+
`call self % [[file(type):open(bound)]]()`
175
+
176
+
#### Arguments
177
+
178
+
None.
179
+
180
+
#### Example
181
+
182
+
```fortran
183
+
program demo_io_file_open
184
+
use forlab_io, only: file
185
+
use stdlib_error, only: check
186
+
type(file), allocatable :: infile
187
+
188
+
infile = file("filename.txt", "r")
189
+
call check(infile%exist(), msg="File not exist: "//infile%filename)
190
+
call infile%open() !! Open file operation
191
+
deallocate(infile)
192
+
193
+
end program demo_io_file_open
194
+
```
195
+
196
+
### `file%countlines`
197
+
198
+
#### Description
199
+
200
+
Counts the number of lines in a txt file, the number of file lines is stored in `file%lines`.
201
+
202
+
#### Status
203
+
204
+
Experimental.
205
+
206
+
#### Class
207
+
208
+
Impure function.
209
+
210
+
#### Syntax
211
+
212
+
`call self % [[file(type):countlines(bound)]]()`
213
+
214
+
#### Arguments
215
+
216
+
None.
217
+
218
+
#### Example
219
+
220
+
```fortran
221
+
program demo_io_file_countlines
222
+
use forlab_io, only: file, disp
223
+
type(file), allocatable :: infile
224
+
225
+
infile = file("filename.txt", "r")
226
+
call infile%countlines() !! Counts the number of lines in a txt file, the number of file lines is stored in `infile%lines`.
227
+
call disp(infile%lines, "The number of file lines : ")
228
+
deallocate(infile)
229
+
230
+
end program demo_io_file_countlines
231
+
```
232
+
233
+
### `file%close`
234
+
235
+
#### Description
236
+
237
+
Closes the `file` object, deallocate `file%filename`.
238
+
239
+
#### Status
240
+
241
+
Experimental.
242
+
243
+
#### Class
244
+
245
+
Impure function.
246
+
247
+
#### Syntax
248
+
249
+
`call self % [[file(type):close(bound)]]()`
250
+
251
+
#### Arguments
252
+
253
+
None.
254
+
255
+
#### Example
256
+
257
+
```fortran
258
+
program demo_io_file_close
259
+
use forlab_io, only: file, disp
260
+
type(file) :: infile
261
+
262
+
infile = file("filename.txt", "r")
263
+
call infile%open()
264
+
call infile%close() !! Closes the `infile` object, deallocate `infile%filename`.
0 commit comments