annotate scripts/io/fileread.m @ 14416:59e20a5e2ca8

fileread() should return a row vector. * fileread.m: Return a row vector.
author Ben Abbott <bpabbott@mac.com>
date Mon, 27 Feb 2012 07:50:26 -0500
parents f3d52523cde1
children 099bd779466c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
14138
72c96de7a403 maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents: 13944
diff changeset
1 ## Copyright (C) 2010-2012 VZLU Prague
10081
debb5b35a1a7 implement fileread
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
2 ##
11104
2c356a35d7f5 fix copyright notices
John W. Eaton <jwe@octave.org>
parents: 10793
diff changeset
3 ## This file is part of Octave.
10081
debb5b35a1a7 implement fileread
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
4 ##
11104
2c356a35d7f5 fix copyright notices
John W. Eaton <jwe@octave.org>
parents: 10793
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
2c356a35d7f5 fix copyright notices
John W. Eaton <jwe@octave.org>
parents: 10793
diff changeset
6 ## under the terms of the GNU General Public License as published by
2c356a35d7f5 fix copyright notices
John W. Eaton <jwe@octave.org>
parents: 10793
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
2c356a35d7f5 fix copyright notices
John W. Eaton <jwe@octave.org>
parents: 10793
diff changeset
8 ## your option) any later version.
2c356a35d7f5 fix copyright notices
John W. Eaton <jwe@octave.org>
parents: 10793
diff changeset
9 ##
2c356a35d7f5 fix copyright notices
John W. Eaton <jwe@octave.org>
parents: 10793
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
2c356a35d7f5 fix copyright notices
John W. Eaton <jwe@octave.org>
parents: 10793
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
2c356a35d7f5 fix copyright notices
John W. Eaton <jwe@octave.org>
parents: 10793
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
2c356a35d7f5 fix copyright notices
John W. Eaton <jwe@octave.org>
parents: 10793
diff changeset
13 ## General Public License for more details.
10081
debb5b35a1a7 implement fileread
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
14 ##
debb5b35a1a7 implement fileread
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
15 ## You should have received a copy of the GNU General Public License
11104
2c356a35d7f5 fix copyright notices
John W. Eaton <jwe@octave.org>
parents: 10793
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
10081
debb5b35a1a7 implement fileread
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
17 ## <http://www.gnu.org/licenses/>.
debb5b35a1a7 implement fileread
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
18
debb5b35a1a7 implement fileread
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
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
debb5b35a1a7 implement fileread
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
23 ## @end deftypefn
debb5b35a1a7 implement fileread
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
24
debb5b35a1a7 implement fileread
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
25 function str = fileread (filename)
debb5b35a1a7 implement fileread
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
26
debb5b35a1a7 implement fileread
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
27 if (nargin != 1)
debb5b35a1a7 implement fileread
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
28 print_usage ();
debb5b35a1a7 implement fileread
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
29 endif
debb5b35a1a7 implement fileread
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
30
debb5b35a1a7 implement fileread
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
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
debb5b35a1a7 implement fileread
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
33 endif
debb5b35a1a7 implement fileread
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
34
debb5b35a1a7 implement fileread
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
35 fid = fopen (filename, "r");
debb5b35a1a7 implement fileread
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
36 if (fid < 0)
debb5b35a1a7 implement fileread
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
37 error ("fileread: cannot open file");
debb5b35a1a7 implement fileread
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
38 endif
debb5b35a1a7 implement fileread
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
39
debb5b35a1a7 implement fileread
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
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
debb5b35a1a7 implement fileread
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
42 unwind_protect_cleanup
10110
c146b308739f missing semicolon in fileread
Jaroslav Hajek <highegg@gmail.com>
parents: 10081
diff changeset
43 fclose (fid);
10081
debb5b35a1a7 implement fileread
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
44 end_unwind_protect
debb5b35a1a7 implement fileread
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
45
debb5b35a1a7 implement fileread
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
46 endfunction
debb5b35a1a7 implement fileread
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
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"};
2b98014771b4 fileread.m: Add functional test.
Rik <octave@nomad.inbox5.com>
parents: 12516
diff changeset
51 %! fname = tmpnam ();
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);
2b98014771b4 fileread.m: Add functional test.
Rik <octave@nomad.inbox5.com>
parents: 12516
diff changeset
56 %! assert (str', [cstr{1} "\n" cstr{2} "\n" cstr{3} "\n"]);
2b98014771b4 fileread.m: Add functional test.
Rik <octave@nomad.inbox5.com>
parents: 12516
diff changeset
57 %! unlink (fname);
2b98014771b4 fileread.m: Add functional test.
Rik <octave@nomad.inbox5.com>
parents: 12516
diff changeset
58
2b98014771b4 fileread.m: Add functional test.
Rik <octave@nomad.inbox5.com>
parents: 12516
diff changeset
59 %% Test input validation
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