本文整理汇总了C++中istream::unget方法的典型用法代码示例。如果您正苦于以下问题:C++ istream::unget方法的具体用法?C++ istream::unget怎么用?C++ istream::unget使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类istream
的用法示例。
在下文中一共展示了istream::unget方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: readline
void readline (istream &cin, string &s) {
while (cin) {
char c;
size_t n=0;
while (cin && cin.get(c) && (c!=10) && (c!=13)) {
if (c==9) {
int nn = 8 - (n%8);
for (int i=0 ; i<nn ; i++) s+=' ', n++;
} else {
s+=c, n++;
}
}
if (!cin) return;
if (c==10) {
if (cin.get(c) && (c!=13)) {
cin.unget();
}
return;
}
if (c==13) {
if (cin.get(c) && (c!=10)) {
cin.unget();
}
return;
}
}
}
示例2: while
//---------------------------------------------------------------------------
unique_ptr<Expression> ExpressionParser::parseSingleExpression(istream& input, ExpressionType lastExpression, Environment& environment)
{
// read
harriet::skipWhiteSpace(input);
char a = input.get();
if(!input.good())
return nullptr;
// other single letter operators
if(a == '(') return make_unique<OpeningPharentesis>();
if(a == ')') return make_unique<ClosingPharentesis>();
if(a == '+') return make_unique<PlusOperator>();
if(a == '-') { if(lastExpression==ExpressionType::TBinaryOperator || lastExpression==ExpressionType::TUnaryOperator || lastExpression==ExpressionType::TOpeningPharentesis) return make_unique<UnaryMinusOperator>(); else return make_unique<MinusOperator>(); }
if(a == '*') return make_unique<MultiplicationOperator>();
if(a == '/') return make_unique<DivisionOperator>();
if(a == '%') return make_unique<ModuloOperator>();
if(a == '^') return make_unique<ExponentiationOperator>();
if(a == '&') return make_unique<AndOperator>();
if(a == '|') return make_unique<OrOperator>();
if(a=='>' && input.peek()!='=') return make_unique<GreaterOperator>();
if(a=='<' && input.peek()!='=') return make_unique<LessOperator>();
if(a=='!' && input.peek()!='=') return make_unique<NotOperator>();
if(a=='=' && input.peek()!='=') return make_unique<AssignmentOperator>();
// check for string
char b = input.get();
if(a=='"') {
string result;
while(b!='"' && a!='\\') {
if(!input.good())
throw harriet::Exception{"unterminated string expression"};
result.push_back(b);
a = b;
b = input.get();
}
return make_unique<StringValue>(result);
}
// check for two signed letters
if(input.good()) {
if(a=='=' && b=='=') return make_unique<EqualOperator>();
if(a=='>' && b=='=') return make_unique<GreaterEqualOperator>();
if(a=='<' && b=='=') return make_unique<LessEqualOperator>();
if(a=='!' && b=='=') return make_unique<NotEqualOperator>();
input.unget();
} else {
input.clear();
}
// check for a number
input.unget();
if(isdigit(a)) {
int32_t intNum;
input >> intNum;
if(input.peek()=='.' && input.good()) {
float floatNum;
input >> floatNum;
return make_unique<FloatValue>(floatNum+intNum);
} else {
return make_unique<IntegerValue>(intNum);
示例3: read
void Fasta_reader::read(istream & input, vector<Fasta_entry> & seqs, bool short_names, bool degap) const throw (Exception)
{
if (!input) { throw IOException ("Fasta_reader::read. Failed to open file"); }
char c = input.get();
while(c==' ' || c=='\n')
{
c = input.get();
}
if(c=='>')
{
input.unget();
this->read_fasta(input,seqs,short_names,degap);
}
else if(c=='@')
{
input.unget();
this->read_fastq(input,seqs);
}
else if(c=='#')
{
input.unget();
this->read_graph(input,seqs,short_names);
}
else
{
Log_output::write_out("Input file format unrecognized. Only FASTA and FASTQ formats supported. Exiting.\n\n",0);
exit(1);
}
map<string,int> copy_num;
for(int i=0;i<(int)seqs.size();i++)
{
string name = seqs.at(i).name;
map<string,int>::iterator it = copy_num.find(name);
if(it != copy_num.end())
{
it->second++;
stringstream ss;
ss<<"."<<it->second;
name.append(ss.str());
Log_output::write_out("Warning: duplicate names found. '"+seqs.at(i).name+"' is renamed '"+name+"'.\n",2);
seqs.at(i).name = name;
}
else
{
copy_num.insert( pair<string,int>(name,0) );
}
}
}
示例4: if
int
readDataLine(istream &f, strings_t &line, int &expected)
{
int obtained = 0;
while (f.good())
{
int c = skipBlank(f);
if (c == '\n' || c == '\r')
break;
string s;
f >> s;
if (! s.empty())
{
line.push_back(s);
obtained += 1;
}
}
int c = f.get();
if (c == '\r' && f.get() != '\n')
f.unget();
if (obtained > 0)
{
if (expected <= 0)
expected = obtained;
else if (expected > 0 && expected != obtained)
{
cerr << "ERROR: expecting " << expected
<< " columns in data file." << endl;
exit(10);
}
}
else
skipSpace(f);
return obtained;
}
示例5: SkipWS
void SkipWS( istream& is)
{
if( is.eof())
throw GDLIOException( "End of file encountered. "+
StreamInfo( &is));
char c;
do {
c = is.get();
if ( (is.rdstate() & ifstream::failbit ) != 0 )
{
if ( (is.rdstate() & ifstream::eofbit ) != 0 )
throw GDLIOException( "End of file encountered. "+
StreamInfo( &is));
if ( (is.rdstate() & ifstream::badbit ) != 0 )
throw GDLIOException( "Error reading stream. "+
StreamInfo( &is));
is.clear();
return ;
}
} while( c == ' ' || c == '\t' || c == '\n');
is.unget();
}
示例6: read
void Polygon::read(istream &stream) {
flags = readString(stream);
readWS(stream);
int c = stream.get();
if (c != ')') THROW("missing ')' on Polygon");
readWS(stream);
c = stream.get();
if (c != '(') THROW("missing '(' on Polygon");
do {
readWS(stream);
c = stream.get();
if (c != '[') break;
Point p;
stream >> p;
points.push_back(p);
readWS(stream);
c = stream.get();
if (c != ']') THROW("missing ']' on Polygon point");
} while (true);
stream.unget();
}
示例7: readWS
void Object::readWS(istream &stream) const {
int c;
do {
c = stream.get();
if (c == -1) return;
} while (isspace(c));
stream.unget();
}
示例8: while
static int
skipSpace(istream &f)
{
int c = f.get();
while (f.good() && isspace(c))
c = f.get();
if (! f.eof())
f.unget();
return c;
}
示例9: SendFormattedInputToOutput
void SendFormattedInputToOutput(istream &input, ostream &output)
{
string line;
while (getline(input, line))
{
output << RemoveExtraSpaces(line);
input.unget();
if (input.get() == '\n')
{
output << endl;
}
}
}
示例10: getHtml
vector<tag> getHtml(istream& input, int htmlLines){
vector<tag> html;
stack<tag> holdTag;
for (size_t i = 0; i < htmlLines; i++) {
tag temp;
input.ignore(LINESIZE, '<');
if (input.peek() == '/') {
tag hold = holdTag.top();
holdTag.pop();
if (holdTag.empty()) {
html.push_back(hold);
}else{
holdTag.top().children.push_back(hold);
}
input.ignore(LINESIZE, '\n');
}else{
holdTag.push(tag());
input >> holdTag.top().tagName;
if(holdTag.top().tagName.back() == '>'){
holdTag.top().tagName.erase(holdTag.top().tagName.end()-1);
input.unget();
//remove the last char if the > char is found there
}
while(input.peek() != '>'){
string key, val;
input.get();
getline(input, key, ' ');
input.ignore(LINESIZE, '\"');
getline(input, val, '\"');
holdTag.top().attributes.insert(make_pair(key, val));
}
}
}
return html;
}
示例11: ReadComplexElement
// no skip of WS
const string ReadComplexElement(istream& is)
{
SkipWS( is);
string buf;
char c = is.get();
if ( (is.rdstate() & ifstream::failbit ) != 0 )
{
if ( (is.rdstate() & ifstream::eofbit ) != 0 )
throw GDLIOException( "End of file encountered. "+
StreamInfo( &is));
if ( (is.rdstate() & ifstream::badbit ) != 0 )
throw GDLIOException( "Error reading stream. "+
StreamInfo( &is));
is.clear();
return buf;
}
bool brace = (c == '(');
if( !brace)
{
is.unget();
return ReadElement( is);
}
buf.push_back( c);
for(;;)
{
c = is.get();
if ( (is.rdstate() & ifstream::failbit ) != 0 )
{
if ( (is.rdstate() & ifstream::badbit ) != 0 )
throw GDLIOException( "Error reading line. "+
StreamInfo( &is));
is.clear();
return buf;
}
if( c == '\n')
return buf;
buf.push_back( c);
if( c == ')')
return buf;
}
}
示例12: fill_vector
// EOFまたは終了インジゲータが検出されるまでistからvに整数を読み取る
void fill_vector(istream& ist, vector<int>& v, char terminator){
int i = 0;
while(ist >> i) v.push_back(i);
if(ist.eof()) return; // OK: EOFが検出された
if(ist.fail()){ // できるだけ後始末をし、問題を報告する
ist.clear(); // ストリームの状態をクリアし、終了インジゲータを調査できるようにする
char c;
ist >> c; // 文字を読み取る(終了インジゲータでありますように)
if(c != terminator){ // 予想外の文字
ist.unget();
ist.clear(ios_base::failbit); // 状態をfailに設定する
}
}
示例13: ReadDecNumber
TokenType ReadDecNumber (istream& is, string& str, Coordinates& xy)
{
State st = is_digit;
TokenType T = int_const_dec;
bool point = false;
bool exp = false;
while (st == is_digit)
{
st = GetState(is.peek());
if (st == is_point)
{
if (point)
throw Error("wrong float number", pos.first, pos.second);
point = true;
T = float_const;
str = str + GetSymb(is,xy);
st = GetState(is.peek());
if (st == is_point)
{
is.unget();
--xy.first;
str.resize(str.size()-1);
return int_const_dec;
}
}
if (is.peek() == 'e' || is.peek() == 'E')
{
if (exp)
throw Error("wrong float number", pos.first, pos.second);
exp = true;
T = float_const;
str = str + GetSymb(is, xy);
st = GetState(is.peek());
if (st == is_plus || st == is_minus)
{
str = str + GetSymb(is, xy);
st = GetState(is.peek());
}
}
st = GetState(is.peek());
if (st == is_digit)
str = str + GetSymb(is, xy);
if (IsSyntaxError(st) || st == is_letter)
throw Error("syntax error", pos.first, pos.second);
}
return T;
}
示例14: fill_vector
void fill_vector(istream& ist, vector<int>& v, char terminator)
// read integers from ist into v until we reach eof() or terminator
{
int i = 0;
while (ist >> i) v.push_back(i);
if (ist.eof()) return; // fine: we found the end of file
// not good() and not bad() and not eof(), ist must be fail()
ist.clear(); // clear stream state
char c;
ist>>c; // read a character, hopefully terminator
if (c != terminator) { // ouch: not the terminator, so we must fail
ist.unget(); // maybe my caller can use that character
ist.clear(ios_base::failbit); // set the state to fail()
}
}
示例15: fillVector
void fillVector(vector<int> &v, istream &ist, char terminator)
{
int x;
while(ist >> x)
v.push_back(x);
if(ist.bad())
error("Some unusual error occurred, stream is in bad state.");
if(ist.eof())
return;
if(ist.fail()) {
ist.clear(); // clear stream state
char c;
ist >> c;
if(c == terminator) {
cout << "found terminator\n";
return;
}
ist.unget();
ist.clear(ios_base::failbit); // set the state to fail
}