octave-bug-tracker
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Octave-bug-tracker] [bug #56200] sscanf fourth output might be incorrec


From: Rik
Subject: [Octave-bug-tracker] [bug #56200] sscanf fourth output might be incorrect for decimal fields
Date: Sun, 26 May 2019 11:25:56 -0400 (EDT)
User-agent: Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko

Follow-up Comment #2, bug #56200 (project octave):

The issue is in oct-stream.cc.


  template <typename 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;
  }


When the format specifier contains a width, Octave reads WIDTH characters from
the input stream and places them in to a temporary input stringstream object. 
It then decodes that buffer with octave_scan_1.

However, the temporary read has altered the state of the istream object that
is an input to this function.  In the case of


sscanf ('3a', '%6d', 1)


the attempted read of 6 characters advances the internal position pointer to
the end of the string, and sets the eof flag on the stream.

One obvious way around this would be to manually set the position and flags of
the stream 'is' based on the values of the position and flags in the temporary
stringstream 'ss'.  In practice, this is going to make the code look pretty
abstruse.

Maybe the solution is to update octave_scan_1 so that it can handle the width
parameter of a format and we avoid creating a temporary istringstream object
entirely.

The 



    _______________________________________________________

Reply to this item at:

  <https://savannah.gnu.org/bugs/?56200>

_______________________________________________
  Message sent via Savannah
  https://savannah.gnu.org/




reply via email to

[Prev in Thread] Current Thread [Next in Thread]