Mercurial > hg > octave-nkf
annotate scripts/io/fileread.m @ 20830:b65888ec820e draft default tip gccjit
dmalcom gcc jit import
author | Stefan Mahr <dac922@gmx.de> |
---|---|
date | Fri, 27 Feb 2015 16:59:36 +0100 |
parents | 9fc020886ae9 |
children |
rev | line source |
---|---|
19898
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
19470
diff
changeset
|
1 ## Copyright (C) 2010-2015 VZLU Prague |
10081 | 2 ## |
11104 | 3 ## This file is part of Octave. |
10081 | 4 ## |
11104 | 5 ## Octave is free software; you can redistribute it and/or modify it |
6 ## under the terms of the GNU General Public License as published by | |
7 ## the Free Software Foundation; either version 3 of the License, or (at | |
8 ## your option) any later version. | |
9 ## | |
10 ## Octave is distributed in the hope that it will be useful, but | |
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 ## General Public License for more details. | |
10081 | 14 ## |
15 ## You should have received a copy of the GNU General Public License | |
11104 | 16 ## along with Octave; see the file COPYING. If not, see |
10081 | 17 ## <http://www.gnu.org/licenses/>. |
18 | |
19 ## -*- texinfo -*- | |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10110
diff
changeset
|
20 ## @deftypefn {Function File} {@var{str} =} fileread (@var{filename}) |
12516
3d6584617da0
Add fileread to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
21 ## Read the contents of @var{filename} and return it as a string. |
3d6584617da0
Add fileread to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
22 ## @seealso{fread, textread, sscanf} |
10081 | 23 ## @end deftypefn |
24 | |
25 function str = fileread (filename) | |
26 | |
27 if (nargin != 1) | |
28 print_usage (); | |
29 endif | |
30 | |
31 if (! ischar (filename)) | |
13767
2b98014771b4
fileread.m: Add functional test.
Rik <octave@nomad.inbox5.com>
parents:
12516
diff
changeset
|
32 error ("fileread: FILENAME argument must be a string"); |
10081 | 33 endif |
34 | |
35 fid = fopen (filename, "r"); | |
36 if (fid < 0) | |
37 error ("fileread: cannot open file"); | |
38 endif | |
39 | |
40 unwind_protect | |
14416
59e20a5e2ca8
fileread() should return a row vector.
Ben Abbott <bpabbott@mac.com>
parents:
14363
diff
changeset
|
41 str = (fread (fid, "*char")).'; |
10081 | 42 unwind_protect_cleanup |
10110
c146b308739f
missing semicolon in fileread
Jaroslav Hajek <highegg@gmail.com>
parents:
10081
diff
changeset
|
43 fclose (fid); |
10081 | 44 end_unwind_protect |
45 | |
46 endfunction | |
47 | |
13767
2b98014771b4
fileread.m: Add functional test.
Rik <octave@nomad.inbox5.com>
parents:
12516
diff
changeset
|
48 |
2b98014771b4
fileread.m: Add functional test.
Rik <octave@nomad.inbox5.com>
parents:
12516
diff
changeset
|
49 %!test |
2b98014771b4
fileread.m: Add functional test.
Rik <octave@nomad.inbox5.com>
parents:
12516
diff
changeset
|
50 %! cstr = {"Hello World", "The answer is 42", "Goodbye World"}; |
19470
6ca096827123
Use tempname() rather than tmpnam() in core Octave.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
51 %! fname = tempname (); |
13944
c9c063c32f99
test: Fix failing test in fileread.m due to EOL issues (Bug #34897)
Rik <octave@nomad.inbox5.com>
parents:
13767
diff
changeset
|
52 %! fid = fopen (fname, "w"); |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
53 %! fprintf (fid, "%s\n", cstr{:}); |
13767
2b98014771b4
fileread.m: Add functional test.
Rik <octave@nomad.inbox5.com>
parents:
12516
diff
changeset
|
54 %! fclose (fid); |
2b98014771b4
fileread.m: Add functional test.
Rik <octave@nomad.inbox5.com>
parents:
12516
diff
changeset
|
55 %! str = fileread (fname); |
16974
221e71d2aef0
Unlink tmp files in %!tests before doing asserts so that file is always removed.
Rik <rik@octave.org>
parents:
14428
diff
changeset
|
56 %! unlink (fname); |
14428
099bd779466c
test: Fix failing %!test in fileread.m
Rik <octave@nomad.inbox5.com>
parents:
14416
diff
changeset
|
57 %! assert (str, [cstr{1} "\n" cstr{2} "\n" cstr{3} "\n"]); |
13767
2b98014771b4
fileread.m: Add functional test.
Rik <octave@nomad.inbox5.com>
parents:
12516
diff
changeset
|
58 |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
59 ## Test input validation |
13767
2b98014771b4
fileread.m: Add functional test.
Rik <octave@nomad.inbox5.com>
parents:
12516
diff
changeset
|
60 %!error fileread () |
2b98014771b4
fileread.m: Add functional test.
Rik <octave@nomad.inbox5.com>
parents:
12516
diff
changeset
|
61 %!error fileread (1, 2) |
2b98014771b4
fileread.m: Add functional test.
Rik <octave@nomad.inbox5.com>
parents:
12516
diff
changeset
|
62 %!error <FILENAME argument must be a string> fileread (1) |
2b98014771b4
fileread.m: Add functional test.
Rik <octave@nomad.inbox5.com>
parents:
12516
diff
changeset
|
63 |