Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

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

      From cppreference.com
      <cpp‎ |io‎ |basic istream
       
       
       
       
      int_type get();
      (1)
      basic_istream& get( char_type& ch);
      (2)
      basic_istream& get( char_type* s,std::streamsize count);
      (3)
      basic_istream& get( char_type* s,std::streamsize count, char_type delim);
      (4)
      basic_istream& get( basic_streambuf& strbuf);
      (5)
      basic_istream& get( basic_streambuf& strbuf, char_type delim);
      (6)

      Extracts character or characters from stream.

      All versions behave asUnformattedInputFunctions. After constructing and checking the sentry object, these functions perform the following:

      1) Reads one character and returns it if available. Otherwise, returnsTraits::eof() and setsfailbit andeofbit.
      2) Reads one character and stores it toch if available. Otherwise, leavesch unmodified and setsfailbit andeofbit. Note that this function is not overloaded on the typessignedchar andunsignedchar, unlike the formatted character input operator>>.
      3) Same asget(s, count, widen('\n')), that is, reads at moststd::max(0, count-1) characters and stores them into character string pointed to bys until'\n' is found.
      4) Reads characters and stores them into the successive locations of the character array whose first element is pointed to bys. Characters are extracted and stored until any of the following occurs:
      • count is less than1 orcount-1 characters have been stored.
      • end of file condition occurs in the input sequence (setstate(eofbit) is called).
      • the next available input characterc equalsdelim, as determined byTraits::eq(c, delim). This character is not extracted (unlikegetline()).
      In any case, ifcount>0, a null character (CharT() is stored in the next successive location of the array.
      5) Same asget(strbuf, widen('\n')), that is, reads available characters and inserts them to the givenbasic_streambuf object until'\n' is found.
      6) Reads characters and inserts them to the output sequence controlled by the givenbasic_streambuf object. Characters are extracted and inserted intostrbuf until any of the following occurs:
      • end of file condition occurs in the input sequence.
      • inserting into the output sequence fails (in which case the character that could not be inserted, is not extracted).
      • the next available input characterc equalsdelim, as determined byTraits::eq(c, delim). This character is not extracted.
      • an exception occurs (in which case the exception is caught and not rethrown).

      If no characters were extracted, callssetstate(failbit).

      All versions set the value ofgcount() to the number of characters extracted.

      Contents

      [edit]Parameters

      ch - reference to the character to write the result to
      s - pointer to the character string to store the characters to
      count - size of character string pointed to bys
      delim - delimiting character to stop the extraction at. It is not extracted and not stored
      strbuf - stream buffer to read the content to

      [edit]Return value

      1) The extracted character orTraits::eof().
      2-6)*this

      [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]Example

      Run this code
      #include <iostream>#include <sstream> int main(){std::istringstream s1("Hello, world.");char c1= s1.get();// reads 'H'std::cout<<"after reading "<< c1<<", gcount() == "<<  s1.gcount()<<'\n'; char c2;    s1.get(c2);// reads 'e'char str[5];    s1.get(str,5);// reads "llo,"std::cout<<"after reading "<< str<<", gcount() == "<<  s1.gcount()<<'\n'; std::cout<< c1<< c2<< str;    s1.get(*std::cout.rdbuf());// reads the rest, not including '\n'std::cout<<"\nAfter the last get(), gcount() == "<< s1.gcount()<<'\n';}

      Output:

      after reading H, gcount() == 1after reading llo,, gcount() == 4Hello, world.After the last get(), gcount() == 7

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 370C++98the effect of overload(5) wasget(s, count, widen('\n')),
      which is the effect of overload(3)
      corrected to
      get(strbuf, widen('\n'))
      LWG 531C++98overloads(3,4) could not handle the
      case wherecount is non-positive
      no character is
      extracted in this case

      [edit]See also

      extracts blocks of characters
      (public member function)[edit]
      extracts formatted data
      (public member function)[edit]
      extracts characters and character arrays
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/io/basic_istream/get&oldid=158516"

      [8]ページ先頭

      ©2009-2025 Movatter.jp