ParaMonte MATLAB 3.0.0
Parallel Monte Carlo and Machine Learning Library
See the latest version documentation.
read_write_entire_textfile.m
Go to the documentation of this file.
1%READ_WRITE_ENTIRE_TEXTFILE Read or write a whole text file to/from memory
2%
3% Read or write an entire text file to/from memory, without leaving the
4% file open if an error occurs.
5%
6% Reading:
7% fstrm = read_write_entire_textfile(fname)
8% Writing:
9% read_write_entire_textfile(fname, fstrm)
10%
11%IN:
12% fname - Pathname of text file to be read in.
13% fstrm - String to be written to the file, including carriage returns.
14%
15%OUT:
16% fstrm - String read from the file. If an fstrm input is given the
17% output is the same as that input.
18
19function fstrm = read_write_entire_textfile(fname, fstrm)
20modes = {'rt', 'wt'};
21writing = nargin > 1;
22fh = fopen(fname, modes{1+writing});
23if fh == -1
24 error('Unable to open file %s.', fname);
25end
26try
27 if writing
28 fwrite(fh, fstrm, 'char*1');
29 else
30 fstrm = fread(fh, '*char')';
31 end
32catch ex
33 fclose(fh);
34 rethrow(ex);
35end
36fclose(fh);
37end
function read_write_entire_textfile(in fname, in fstrm)