[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
scanf and maximum field width
From: |
John W. Eaton |
Subject: |
scanf and maximum field width |
Date: |
Thu, 28 Jun 2007 13:32:42 -0400 |
On 28-Jun-2007, "G. B?rger" wrote:
| I noticed that for
|
| [i1 i2 c] = sscanf("12", "%1i%1i", "C")
|
| octave returns
|
| i1 = 12
| i2 = [](0x0)
| c = 1
|
| I would have assumed that instead
|
| i1 = 1
| i2 = 2
| c = 2
|
| is returned, no?
Please try the following patch.
Please report bugs to the address@hidden list.
jwe
src/ChangeLog:
2007-06-28 John W. Eaton <address@hidden>
* oct-stream.cc (octave_scan_1): New function
(octave_scan): Use it. Handle fmt.width.
Index: src/oct-stream.cc
===================================================================
RCS file: /cvs/octave/src/oct-stream.cc,v
retrieving revision 1.142
diff -u -u -r1.142 oct-stream.cc
--- src/oct-stream.cc 25 Jun 2007 19:06:43 -0000 1.142
+++ src/oct-stream.cc 28 Jun 2007 17:29:20 -0000
@@ -1047,12 +1047,9 @@
#define OCTAVE_SCAN(is, fmt, arg) octave_scan (is, fmt, arg)
-// FIXME -- this needs to be fixed to handle formats which
-// specify a maximum width.
-
template <class T>
std::istream&
-octave_scan (std::istream& is, const scanf_format_elt& fmt, T* valptr)
+octave_scan_1 (std::istream& is, const scanf_format_elt& fmt, T* valptr)
{
T& ref = *valptr;
@@ -1108,6 +1105,30 @@
return is;
}
+template <class T>
+std::istream&
+octave_scan (std::istream& is, const scanf_format_elt& fmt, T* valptr)
+{
+ if (fmt.width)
+ {
+ // Limit input to fmt.width characters by reading into a
+ // temporary stringstream buffer.
+
+ std::string tmp;
+
+ is.width (fmt.width);
+ is >> tmp;
+
+ std::istringstream ss (tmp);
+
+ octave_scan_1 (ss, fmt, valptr);
+ }
+ else
+ octave_scan_1 (is, fmt, valptr);
+
+ return is;
+}
+
// Note that this specialization is only used for reading characters, not
// character strings. See BEGIN_S_CONVERSION for details.