Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::basic_istream<CharT,Traits>::readsome

      From cppreference.com
      <cpp‎ |io‎ |basic istream
       
       
       
       
      std::streamsize readsome( char_type* s,std::streamsize count);

      Extracts up tocount immediately available characters from the input stream. The extracted characters are stored into the character array pointed to bys.

      Behaves asUnformattedInputFunction. After constructing and checking the sentry object,

      • Ifrdbuf()->in_avail()==-1, callssetstate(eofbit) and extracts no characters.
      • Ifrdbuf()->in_avail()==0, extracts no characters.
      • Ifrdbuf()->in_avail()>0, extractsstd::min(rdbuf()->in_avail(), count) characters and stores them into successive locations of the character array whose first element is pointed to bys.

      Contents

      [edit]Parameters

      s - pointer to the character array to store the characters to
      count - maximum number of characters to read

      [edit]Return value

      The number of characters actually extracted.

      [edit]Exceptions

      [edit]
      failure if an error occurred (the error state flag is notgoodbit) andexceptions() is set to throw for that state.

      If an internal operation throws an exception, it is caught andbadbit is set. Ifexceptions() is set forbadbit, the exception is rethrown.

      [edit]Notes

      The behavior of this function is highly implementation-specific. For example, usingreadsome() withstd::ifstream leads to significant, implementation-specific outcomes. Some library implementations fill the underlyingfilebuf with data as soon asstd::ifstream opens a file, which meansreadsome() always reads data and could even read the entire file. With other implementations,std::ifstream only reads from a file when an input operation is invoked, which means callingreadsome() immediately after opening the file never extracts any characters. Similarly, callingstd::cin.readsome() may return all pending, unprocessed console input or may always return zero and extract no characters.

      [edit]Example

      Run this code
      #include <cassert>#include <iostream>#include <sstream> int main(){char c[10]="*********";// c[9] == '\0' // std::stringbuf makes its entire buffer available for unblocking readstd::istringstream input("This is sample text."); auto r= input.readsome(c,5);// reads 'This ' and stores in c[0] .. c[4]assert(r==5);std::cout<< c<<'\n';     r= input.readsome(c,9);// reads 'is sample' and stores in c[0] .. c[8]assert(r==9);std::cout<< c<<'\n';}

      Output:

      This ****is sample

      [edit]See also

      extracts blocks of characters
      (public member function)[edit]
      obtains the number of characters immediately available in the get area
      (public member function ofstd::basic_streambuf<CharT,Traits>)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/io/basic_istream/readsome&oldid=176673"

      [8]ページ先頭

      ©2009-2025 Movatter.jp