0% found this document useful (0 votes)
149 views

Istream - Get - C++ Reference

The istream::get function extracts characters from the input stream in various ways: (1) It can extract a single character, (2) extract characters into a c-string up to a delimiter, or (3) extract characters into a stream buffer object up to a delimiter. It returns the extracted character, or returns itself to allow chaining. It sets error flags if an error occurs or no characters were extracted.

Uploaded by

Azka Ghaffar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
149 views

Istream - Get - C++ Reference

The istream::get function extracts characters from the input stream in various ways: (1) It can extract a single character, (2) extract characters into a c-string up to a delimiter, or (3) extract characters into a stream buffer object up to a delimiter. It returns the extracted character, or returns itself to allow chaining. It sets error flags if an error occurs or no characters were extracted.

Uploaded by

Azka Ghaffar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

12/15/2015

istream::getC++Reference
Search:
Reference

Go
<istream>

istream

get

Notloggedin

register

login

C++
Information
Tutorials
Reference
Articles
Forum

Reference
Clibrary:
Containers:
Input/Output:
<fstream>
<iomanip>
<ios>
<iosfwd>
<iostream>
<istream>
<ostream>
<sstream>
<streambuf>
Multithreading:
Other:

<istream>
classtemplates:
basic_iostream
basic_istream
classes:
iostream
istream
wiostream
wistream
manipulators:
ws

istream
istream::istream
istream::~istream
memberclasses:
istream::sentry
memberfunctions:
istream::gcount
istream::get
istream::getline
istream::ignore
istream::operator>>
istream::peek
istream::putback
istream::read
istream::readsome
istream::seekg
istream::sync
istream::tellg
istream::unget
nonmemberoverloads:
operator>>(istream)
protectedmembers:
istream::operator=
istream::swap

publicmemberfunction
std::

istream::get
singlecharacter(1)
cstring(2)
streambuffer(3)

<istream><iostream>
intget();
istream&get(char&c);
istream&get(char*s,streamsizen);
istream&get(char*s,streamsizen,chardelim);
istream&get(streambuf&sb);
istream&get(streambuf&sb,chardelim);

Getcharacters
Extractscharactersfromthestream,asunformattedinput:
(1)singlecharacter
Extractsasinglecharacterfromthestream.
Thecharacteriseitherreturned(firstsignature),orsetasthevalueofitsargument(secondsignature).
(2)cstring
Extractscharactersfromthestreamandstorestheminsasacstring,untileither(n1)charactershavebeen
extractedorthedelimitingcharacterisencountered:thedelimitingcharacterbeingeitherthenewlinecharacter
('\n')ordelim(ifthisargumentisspecified).
Thedelimitingcharacterisnotextractedfromtheinputsequenceiffound,andremainsthereasthenext
charactertobeextractedfromthestream(seegetlineforanalternativethatdoesdiscardthedelimiting
character).
Anullcharacter('\0')isautomaticallyappendedtothewrittensequenceifnisgreaterthanzero,evenifan
emptystringisextracted.
(3)streambuffer
Extractscharactersfromthestreamandinsertsthemintotheoutputsequencecontrolledbythestreambuffer
objectsb,stoppingeitherassoonassuchaninsertionfailsorassoonasthedelimitingcharacterisencounteredin
theinputsequence(thedelimitingcharacterbeingeitherthenewlinecharacter,'\n',ordelim,ifthisargumentis
specified).
Onlythecharacterssuccessfullyinsertedintosbareextractedfromthestream:Neitherthedelimitingcharacter,
noreventuallythecharacterthatfailedtobeinsertedatsb,areextractedfromtheinputsequenceandremain
thereasthenextcharactertobeextractedfromthestream.
Thefunctionalsostopsextractingcharactersiftheendoffileisreached.Ifthisisreachedprematurely(beforemeeting
theconditionsdescribedabove),thefunctionsetstheeofbitflag.
Internally,thefunctionaccessestheinputsequencebyfirstconstructingasentryobject(withnoskipwssettotrue).
Then(ifgood),itextractscharactersfromitsassociatedstreambufferobjectasifcallingitsmemberfunctionssbumpcor
sgetc,andfinallydestroysthesentryobjectbeforereturning.
Thenumberofcharacterssuccessfullyreadandstoredbythisfunctioncanbeaccessedbycallingmembergcount.

Parameters
c

Thereferencetoacharacterwheretheextractedvalueisstored.

Pointertoanarrayofcharacterswhereextractedcharactersarestoredasacstring.
Ifthefunctiondoesnotextractanycharacters(orifthefirstcharacterextractedisthedelimitercharacter)andn
isgreaterthanzero,thisissettoanemptycstring.

Maximumnumberofcharacterstowritetos(includingtheterminatingnullcharacter).
Ifthisislessthan2,thefunctiondoesnotextractanycharactersandsetsfailbit.
streamsizeisasignedintegraltype.

delim

sb

Explicitdelimitingcharacter:Theoperationofextractingsuccessivecharactersstopsassoonasthenextcharacter
toextractcomparesequaltothis.
Astreambufobjectonwhosecontrolledoutputsequencethecharactersarecopied.

ReturnValue
Thefirstsignaturereturnsthecharacterread,ortheendoffilevalue(EOF)ifnocharactersareavailableinthestream
(notethatinthiscase,thefailbitflagisalsoset).
Allothersignaturesalwaysreturn*this.Notethatthisreturnvaluecanbecheckedforthestateofthestream(see
castingastreamtoboolformoreinfo).
Errorsaresignaledbymodifyingtheinternalstateflags:
flag
error
Thefunctionstoppedextractingcharactersbecausetheinputsequencehasnomorecharactersavailable(end
eofbit
offilereached).
failbit Eithernocharacterswerewrittenoranemptycstringwasstoredins.
Erroronstream(suchaswhenthisfunctioncatchesanexceptionthrownbyaninternaloperation).
badbit
Whenset,theintegrityofthestreammayhavebeenaffected.

http://www.cplusplus.com/reference/istream/istream/get/

1/2

12/15/2015

istream::getC++Reference
Multipleflagsmaybesetbyasingleoperation.
Iftheoperationsetsaninternalstateflagthatwasregisteredwithmemberexceptions,thefunctionthrowsanexception
ofmembertypefailure.

Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

//istream::getexample
#include<iostream>//std::cin,std::cout
#include<fstream>//std::ifstream

Edit

intmain(){
charstr[256];
std::cout<<"Enterthenameofanexistingtextfile:";
std::cin.get(str,256);//getcstring
std::ifstreamis(str);//openfile
charc;
while(is.get(c))//loopgettingsinglecharacters
std::cout<<c;
is.close();//closefile
return0;
}

Thisexamplepromptsforthenameofanexistingtextfileandprintsitscontentonthescreen,usingcin.getbothtoget
individualcharactersandcstrings.

Dataraces
Modifiesc,sbortheelementsinthearraypointedbys.
Modifiesthestreamobject.
Concurrentaccesstothesamestreamobjectmaycausedataraces,exceptforthestandardstreamobjectcinwhenthis
issynchronizedwithstdio(inthiscase,nodataracesareinitiated,althoughnoguaranteesaregivenontheorderin
whichextractedcharactersareattributedtothreads).

Exceptionsafety
Basicguarantee:ifanexceptionisthrown,theobjectisinavalidstate.
Itthrowsanexceptionofmembertypefailureiftheresultingerrorstateflagisnotgoodbitandmemberexceptions
wassettothrowforthatstate.
Anyexceptionthrownbyaninternaloperationiscaughtandhandledbythefunction,settingbadbit.Ifbadbitwasset
onthelastcalltoexceptions,thefunctionrethrowsthecaughtexception.

Seealso
istream::getline

Getline(publicmemberfunction)

istream::ignore

Extractanddiscardcharacters(publicmemberfunction)

istream::gcount

Getcharactercount(publicmemberfunction)

Homepage|Privacypolicy
cplusplus.com,20002015Allrightsreservedv3.1
Spottedanerror?contactus

http://www.cplusplus.com/reference/istream/istream/get/

2/2

You might also like