Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/io/basic filebuf/underflow"

From cppreference.com
< cpp‎ | io‎ | basic filebuf
(+example and seealsos)
m (fmt, ., langlinks)
 
Line 1: Line 1:
{{cpp/io/basic_filebuf/title | underflow }}
+
{{cpp/io/basic_filebuf/title|underflow}}
 
{{cpp/io/basic_filebuf/navbar}}
 
{{cpp/io/basic_filebuf/navbar}}
{{ddcl |
+
{{ddcl|
 
protected:
 
protected:
 
virtual int_type underflow()
 
virtual int_type underflow()
Line 8: Line 8:
 
Reads more data into the input area.  
 
Reads more data into the input area.  
  
Behaves like the base class {{c|std::basic_streambuf::underflow}}, except that to read the data from the associated character sequence (the file) into the get area, first reads the bytes from the file into a temporary buffer (allocated as large as necessary), then uses {{c|std::codecvt::in}} of the imbued locale to convert the external (typically, multibyte) representation to the internal form which is then used to populate the get area. The conversion may be skipped if the locale's {{c|std::codecvt::always_noconv}} returns {{c|true}}
+
Behaves like the base class {{c|std::basic_streambuf::underflow}}, except that to read the data from the associated character sequence (the file) into the get area, first reads the bytes from the file into a temporary buffer (allocated as large as necessary), then uses {{c|std::codecvt::in}} of the imbued locale to convert the external (typically, multibyte) representation to the internal form which is then used to populate the get area. The conversion may be skipped if the locale's {{c|std::codecvt::always_noconv}} returns {{c|true}}
  
 
===Parameters===
 
===Parameters===
Line 18: Line 18:
 
===Example===
 
===Example===
 
{{example
 
{{example
|
+
|code=
| code=
+
 
#include <fstream>
 
#include <fstream>
 
#include <iostream>
 
#include <iostream>
Line 25: Line 24:
 
struct mybuf : std::filebuf
 
struct mybuf : std::filebuf
 
{
 
{
     int underflow() {
+
     int underflow()
 +
{
 
         std::cout << "Before underflow(): size of the get area is "
 
         std::cout << "Before underflow(): size of the get area is "
 
                   << egptr()-eback() << " with "
 
                   << egptr()-eback() << " with "
Line 43: Line 43:
 
     buf.open("test.txt", std::ios_base::in);
 
     buf.open("test.txt", std::ios_base::in);
 
     std::istream stream(&buf);
 
     std::istream stream(&buf);
     while(stream.get()) ;
+
     while (stream.get()) ;
 
}
 
}
| p=true
+
|p=true
| output=
+
|output=
 
Before underflow(): size of the get area is 0 with 0 read positions available
 
Before underflow(): size of the get area is 0 with 0 read positions available
 
underflow() returns 73.
 
underflow() returns 73.
Line 57: Line 57:
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/io/basic_streambuf/dsc underflow}}
+
{{dsc inc|cpp/io/basic_streambuf/dsc underflow}}
{{dsc inc | cpp/io/basic_stringbuf/dsc underflow}}
+
{{dsc inc|cpp/io/basic_stringbuf/dsc underflow}}
{{dsc inc | cpp/io/strstreambuf/dsc underflow}}
+
{{dsc inc|cpp/io/strstreambuf/dsc underflow}}
{{dsc inc | cpp/io/basic_filebuf/dsc uflow}}
+
{{dsc inc|cpp/io/basic_filebuf/dsc uflow}}
{{dsc inc | cpp/io/basic_filebuf/dsc overflow}}
+
{{dsc inc|cpp/io/basic_filebuf/dsc overflow}}
{{dsc inc | cpp/io/basic_streambuf/dsc sgetc}}
+
{{dsc inc|cpp/io/basic_streambuf/dsc sgetc}}
 
{{dsc end}}
 
{{dsc end}}
  
[[de:cpp/io/basic filebuf/underflow]]
+
deesfritjaptruzh
[[es:cpp/io/basic filebuf/underflow]]
+
[[fr:cpp/io/basic filebuf/underflow]]
+
[[it:cpp/io/basic filebuf/underflow]]
+
[[ja:cpp/io/basic filebuf/underflow]]
+
[[pt:cpp/io/basic filebuf/underflow]]
+
[[ru:cpp/io/basic filebuf/underflow]]
+
[[zh:cpp/io/basic filebuf/underflow]]
+

Latest revision as of 00:54, 7 September 2023

 
 
 
 
protected:
virtual int_type underflow()

Reads more data into the input area.

Behaves like the base class std::basic_streambuf::underflow, except that to read the data from the associated character sequence (the file) into the get area, first reads the bytes from the file into a temporary buffer (allocated as large as necessary), then uses std::codecvt::in of the imbued locale to convert the external (typically, multibyte) representation to the internal form which is then used to populate the get area. The conversion may be skipped if the locale's std::codecvt::always_noconv returns true.

Contents

[edit] Parameters

(none)

[edit] Return value

Traits::to_int_type(*gptr()) (the first character of the pending sequence) in case of success, or Traits::eof() in case of failure.

[edit] Example

#include <fstream>
#include <iostream>
 
struct mybuf : std::filebuf
{
    int underflow()
    {
         std::cout << "Before underflow(): size of the get area is "
                   << egptr()-eback() << " with "
                   << egptr()-gptr() << " read positions available\n";
         int rc = std::filebuf::underflow();
         std::cout << "underflow() returns " << rc << ".\nAfter the call, "
                   << "size of the get area is "
                   << egptr()-eback() << " with "
                   << egptr()-gptr() << " read positions available\n";
        return rc;
    }
};
 
int main()
{
    mybuf buf;
    buf.open("test.txt", std::ios_base::in);
    std::istream stream(&buf);
    while (stream.get()) ;
}

Possible output:

Before underflow(): size of the get area is 0 with 0 read positions available
underflow() returns 73.
After the call, size of the get area is 110 with 110 read positions available
Before underflow(): size of the get area is 110 with 0 read positions available
underflow() returns -1.
After the call, size of the get area is 0 with 0 read positions available

[edit] See also

[virtual]
reads characters from the associated input sequence to the get area
(virtual protected member function of std::basic_streambuf<CharT,Traits>) [edit]
[virtual]
returns the next character available in the input sequence
(virtual protected member function of std::basic_stringbuf<CharT,Traits,Allocator>) [edit]
[virtual]
reads a character from the input sequence without advancing the next pointer
(virtual protected member function of std::strstreambuf) [edit]
[virtual]
reads from the associated file and advances the next pointer in the get area
(virtual protected member function) [edit]
[virtual]
writes characters to the associated file from the put area
(virtual protected member function) [edit]
reads one character from the input sequence without advancing the sequence
(public member function of std::basic_streambuf<CharT,Traits>) [edit]