comparison src/file-io.cc @ 13271:fba2cc36b762

return stream error message in scanf functions and document behavior * oct-stream.cc (octave_base_stream::oscanf): Also return stream error message. * file-io.cc (Ffscanf, Fsscanf, Fscanf): Return stream error message for both vectorized and "C" style calls. Update doc string.
author John W. Eaton <jwe@octave.org>
date Tue, 04 Oct 2011 10:20:24 -0400
parents 3e1871badab9
children 9cae456085c2
comparison
equal deleted inserted replaced
13270:eae58f217102 13271:fba2cc36b762
1066 return retval; 1066 return retval;
1067 } 1067 }
1068 1068
1069 DEFUN (fscanf, args, , 1069 DEFUN (fscanf, args, ,
1070 "-*- texinfo -*-\n\ 1070 "-*- texinfo -*-\n\
1071 @deftypefn {Built-in Function} {[@var{val}, @var{count}] =} fscanf (@var{fid}, @var{template}, @var{size})\n\ 1071 @deftypefn {Built-in Function} {[@var{val}, @var{count}, @var{errmsg}] =} fscanf (@var{fid}, @var{template}, @var{size})\n\
1072 @deftypefnx {Built-in Function} {[@var{v1}, @var{v2}, @dots{}, @var{count}] =} fscanf (@var{fid}, @var{template}, \"C\")\n\ 1072 @deftypefnx {Built-in Function} {[@var{v1}, @var{v2}, @dots{}, @var{count}, @var{errmsg}] =} fscanf (@var{fid}, @var{template}, \"C\")\n\
1073 In the first form, read from @var{fid} according to @var{template},\n\ 1073 In the first form, read from @var{fid} according to @var{template},\n\
1074 returning the result in the matrix @var{val}.\n\ 1074 returning the result in the matrix @var{val}.\n\
1075 \n\ 1075 \n\
1076 The optional argument @var{size} specifies the amount of data to read\n\ 1076 The optional argument @var{size} specifies the amount of data to read\n\
1077 and may be one of\n\ 1077 and may be one of\n\
1099 \n\ 1099 \n\
1100 A string is returned if @var{template} specifies only character\n\ 1100 A string is returned if @var{template} specifies only character\n\
1101 conversions.\n\ 1101 conversions.\n\
1102 \n\ 1102 \n\
1103 The number of items successfully read is returned in @var{count}.\n\ 1103 The number of items successfully read is returned in @var{count}.\n\
1104 \n\
1105 If an error occurs, @var{errmsg} contains a system-dependent error message.\n\
1104 \n\ 1106 \n\
1105 In the second form, read from @var{fid} according to @var{template},\n\ 1107 In the second form, read from @var{fid} according to @var{template},\n\
1106 with each conversion specifier in @var{template} corresponding to a\n\ 1108 with each conversion specifier in @var{template} corresponding to a\n\
1107 single scalar return value. This form is more `C-like', and also\n\ 1109 single scalar return value. This form is more `C-like', and also\n\
1108 compatible with previous versions of Octave. The number of successful\n\ 1110 compatible with previous versions of Octave. The number of successful\n\
1133 ::error ("%s: format TEMPLATE must be a string", who.c_str ()); 1135 ::error ("%s: format TEMPLATE must be a string", who.c_str ());
1134 } 1136 }
1135 } 1137 }
1136 else 1138 else
1137 { 1139 {
1138 retval (1) = 0.0; 1140 retval(2) = "unknown error";
1139 retval (0) = Matrix (); 1141 retval(1) = 0.0;
1142 retval(0) = Matrix ();
1140 1143
1141 if (nargin == 2 || nargin == 3) 1144 if (nargin == 2 || nargin == 3)
1142 { 1145 {
1143 octave_stream os = octave_stream_list::lookup (args(0), who); 1146 octave_stream os = octave_stream_list::lookup (args(0), who);
1144 1147
1156 { 1159 {
1157 octave_value tmp = os.scanf (args(1), size, count, who); 1160 octave_value tmp = os.scanf (args(1), size, count, who);
1158 1161
1159 if (! error_state) 1162 if (! error_state)
1160 { 1163 {
1164 retval(2) = os.error ();
1161 retval(1) = count; 1165 retval(1) = count;
1162 retval(0) = tmp; 1166 retval(0) = tmp;
1163 } 1167 }
1164 } 1168 }
1165 } 1169 }
1191 return retval; 1195 return retval;
1192 } 1196 }
1193 1197
1194 DEFUN (sscanf, args, , 1198 DEFUN (sscanf, args, ,
1195 "-*- texinfo -*-\n\ 1199 "-*- texinfo -*-\n\
1196 @deftypefn {Built-in Function} {[@var{val}, @var{count}, @var{pos}] =} sscanf (@var{string}, @var{template}, @var{size})\n\ 1200 @deftypefn {Built-in Function} {[@var{val}, @var{count}, @var{errmsg}, @var{pos}] =} sscanf (@var{string}, @var{template}, @var{size})\n\
1197 @deftypefnx {Built-in Function} {[@var{v1}, @var{v2}, @dots{}, @var{count}] =} sscanf (@var{string}, @var{template}, \"C\")\n\ 1201 @deftypefnx {Built-in Function} {[@var{v1}, @var{v2}, @dots{}, @var{count}, @var{errmsg}] =} sscanf (@var{string}, @var{template}, \"C\")\n\
1198 This is like @code{fscanf}, except that the characters are taken from the\n\ 1202 This is like @code{fscanf}, except that the characters are taken from the\n\
1199 string @var{string} instead of from a stream. Reaching the end of the\n\ 1203 string @var{string} instead of from a stream. Reaching the end of the\n\
1200 string is treated as an end-of-file condition. In addition to the values\n\ 1204 string is treated as an end-of-file condition. In addition to the values\n\
1201 returned by @code{fscanf}, the index of the next character to be read\n\ 1205 returned by @code{fscanf}, the index of the next character to be read\n\
1202 is returned in in @var{pos}.\n\ 1206 is returned in in @var{pos}.\n\
1288 return retval; 1292 return retval;
1289 } 1293 }
1290 1294
1291 DEFUN (scanf, args, nargout, 1295 DEFUN (scanf, args, nargout,
1292 "-*- texinfo -*-\n\ 1296 "-*- texinfo -*-\n\
1293 @deftypefn {Built-in Function} {[@var{val}, @var{count}] =} scanf (@var{template}, @var{size})\n\ 1297 @deftypefn {Built-in Function} {[@var{val}, @var{count}, @var{errmsg}] =} scanf (@var{template}, @var{size})\n\
1294 @deftypefnx {Built-in Function} {[@var{v1}, @var{v2}, @dots{}, @var{count}]] =} scanf (@var{template}, \"C\")\n\ 1298 @deftypefnx {Built-in Function} {[@var{v1}, @var{v2}, @dots{}, @var{count}, @var{errmsg}]] =} scanf (@var{template}, \"C\")\n\
1295 This is equivalent to calling @code{fscanf} with @var{fid} = @code{stdin}.\n\ 1299 This is equivalent to calling @code{fscanf} with @var{fid} = @code{stdin}.\n\
1296 \n\ 1300 \n\
1297 It is currently not useful to call @code{scanf} in interactive\n\ 1301 It is currently not useful to call @code{scanf} in interactive\n\
1298 programs.\n\ 1302 programs.\n\
1299 @seealso{fscanf, sscanf, printf}\n\ 1303 @seealso{fscanf, sscanf, printf}\n\