本文整理汇总了C++中ifstream::putback方法的典型用法代码示例。如果您正苦于以下问题:C++ ifstream::putback方法的具体用法?C++ ifstream::putback怎么用?C++ ifstream::putback使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ifstream
的用法示例。
在下文中一共展示了ifstream::putback方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: lex
void lex(ifstream& ifs) {
// Reserve
reserve();
// Vector of tokens in one statement (statement ends with semicolon)
vector<Token> oneStmtToken;
char current;
while ( (current = ifs.get()) != EOF ) {
int type = getType(current);
if ( type == SPACE ) {
// Ignore
} else if ( type == DIGIT ) {
// Get the actual number
int v = 0;
int startCol = _col;
do {
v = 10 * v + current - '0';
current = ifs.get();
} while ( getType(current) == DIGIT );
ifs.putback(current);
revert(current);
Token num(NUM, _line, startCol, v);
oneStmtToken.push_back(num);
} else if ( type == LETTER ) {
// Get the id
string buffer;
int subType;
int startCol = _col;
do {
buffer += current;
current = ifs.get();
subType = getType(current);
} while ( subType == LETTER || subType == DIGIT );
ifs.putback(current);
revert(current);
// If it is keyword
int tag = symbol.find(buffer)->second.tag;
if ( tag >= 256 && tag <= 269 ) {
Token keyword(tag, _line, startCol, -1, buffer);
oneStmtToken.push_back(keyword);
} else { // It is identifier
Token keyword(ID, _line, startCol, -1, buffer);
oneStmtToken.push_back(keyword);
}
} else if ( type == DELIMIT ) {
// Convert char to string to be stored in Token
stringstream ss;
ss << current;
string cur;
ss >> cur;
Token delimit(current, _line, _col, -1, cur);
oneStmtToken.push_back(delimit);
} else if ( type == REGEXSTART ) {
示例2: extractToken
void PNMFileUtils::extractToken(ifstream &inputStream,
char *token,
int maxSize)
{
int count = 0;
char ch;
// skip whitespace and comments
do
{
inputStream.read((char *)&ch, sizeof(char));
// if a comment char is found then read till the next "\n"
if(ch == '#')
while(ch != '\n')
inputStream.read((char *)&ch, sizeof(char *));
}
while(ch == ' ' || ch == '\t' || ch == '\n');
// copy data into token
do
{
if(count >= maxSize - 1)
throw runtime_error("Token too large");
token[count++] = ch;
inputStream.read((char *)&ch, sizeof(char));
}
while(ch != ' ' && ch != '\t' && ch != '\n' && ch != '.');
inputStream.putback(ch);
token[count] = '\0';
}
示例3: getStatements
void getStatements(ifstream &file)
{
string token,tmp;
tmp=Peek(file);
if (tmp=="}" || tmp==")") return;
while (true)
{
file >> token;
if (token=="}")
{
file.unget();
return;
}
if (token=="var")
getVarsDeclaration(file);
else
{
//token eh nome
tmp=token;
file >> token; // pega o segundo token depois do nome
putback(token,file); // retorna ponteiro para o nome
do file.putback(1); while (file.peek()==' ');
putback(tmp,file); //retorna ponteiro para antes do token
if (token=="(")
getFuncCall(file);
cout << endl;
}
expect(";",file);
}
}
示例4: getVarsDeclaration
void getVarsDeclaration(ifstream &file)
{ //var ja foi consumido
string token;
//----------------------------
file >> token; // "name?"
file.putback(token.length()); // retorna "name?"
if (token==";")
{
cout << "expected: variable identifier" <<endl;
exit(0);
}
while (token!=";" && file.good())
{
file >> token; // name
file >> token; // separador ou terminador
if (token!="," && token!=";")
{
cout << "expected: variable identifier separator or declaration terminator" <<endl;
exit(0);
}
file.unget(); //putback the ;
}
}
示例5: sizeof
ContractInfoRec::ContractInfoRec(ifstream &ifs, const char * const sf, int line):
Record(ContractInfo, sf, line)
{
ifs.get(Contract, sizeof(Contract));
ContractLetter = Contract[0];
char buf2[2+1];
buf2[0] = Contract[1];
buf2[1] = Contract[2];
buf2[2] = '\0';
BasePrice = atoi(buf2);
buf2[0] = Contract[3];
buf2[1] = Contract[4];
SubLevel = atoi(buf2);
char c;
unsigned int i = 0;
do {
ifs.get(c);
if (i < sizeof(Text)-2)
{
Text[i] = c;
i++;
}
}
while (c != '\n');
if (Text[i-1] == '\n')
Text[i-1] = '\0';
else
Text[i] = '\0';
ifs.putback('\n');
}
示例6: eof
bool eof(ifstream& in)
{
char ch;
in >> ch;
in.putback(ch);
return !in;
}
示例7: skipspaces
void skipspaces() {
char ch;
do {
ch = fin.get();
} while(isspace(ch));
fin.putback(ch);
}
示例8: Define
/*VARIABLES FUNCTION Define: READS AND STORES VARIABLES*/
string VARIABLES::Define(ifstream &Input) {
//DECLARE LOCAL VARIABLES
string Read_Next(ifstream &Input);
string str;
int i;
char c;
const int n = 7;
int delim[n] = {9,10,13,32,44,61,124};/*{'\t','\n','?CR?',' ',',','=','|'}*/
//GET VARIABLE NAME (WITHOUT LETTING Read_Next TRY TO REPLACE)
str.clear();
while (Input.get(c)) {
//Check for comments
if(int(c)=='%') {
Input.ignore(10001, '\n');
continue;
}
//Check for deliminators
for(i=0;i<n;i++) {
if(int(c)==delim[i]) {
i=-1;
break;
}
}
if(i!=-1) {str += c;break;}
}
if(int(c)!='$') {
//Not a valid variable name (return)
Input.putback(c);
return(Read_Next(Input));
}
str = Read_Next(Input);//NOTE: this allows deliminators between '$' and varaible name
str.insert(0, "$");
//FIND VARIABLE TO REDEFINE IT OR FIND UNUSED SLOT
VARIABLES *var = this;
while(var!=var->next) {
if(str.compare(var->symb)==0) {break;}
var = var->next;
}
//STORE SYMBOL AND VALUE
var->symb = str;
str = Read_Next(Input);
i = int(str[0]);
if( (i<43)||(i>57)||(i==44)||(i==47) ) {//Check if a number is present
Log <<str<<" is not a valid expression for variable "<<var->symb<<endl;
exit(0);
}
var->value = atof(str.c_str());
if(var==var->next) {var->next = new VARIABLES();}//Allocate memory for next variable
return(Define(Input));
}
示例9: SaltarSeparadores
char SaltarSeparadores (ifstream& f)
{
char c;
do {
c= f.get();
} while (isspace(c));
f.putback(c);
return c;
}
示例10: pushBackPointer
// push the file read pointer one step backward
void scanner::pushBackPointer(char ch)
{
if(ch=='\n')
{
linenum--;
}
fin.putback(ch);
}
示例11: getValeur
float getValeur(ifstream &fichier, char caractere)
{
float temp = 0;
fichier.putback(caractere);
fichier>>temp;
return temp;
}
示例12: buildMap
void buildMap(ifstream& input, Map<string, Vector<char> >& _Map_, int& orderK) {
char ch;
while(input.get(ch)) {
input.putback(ch);
Vector<char> associatedValues; /* All the values associated with a key. */
Vector<char> rewindValues; /* Holds chars to be put back into stream. */
string newKey = "";
/* Create a new key by appending as many letters as K value. */
addToString(input, ch, newKey, rewindValues, orderK);
/* Add to the key Map. */
addToKey(_Map_, associatedValues, newKey, input);
/* Put back the characters into ifstream from reverse. */
for(int i=orderK-1; i>0; i--) {
input.putback(rewindValues[i]);
}
}
}
示例13: Term2
int Term2(int inp)
{
int result = inp;
char a;
testfile.get(a);
if(a != EOF)
{
if (a == '*')
result = Term2(result * Fact());
else if (a == '/')
result = Term2(result / Fact());
else if (a == '+' || a == '-')
testfile.putback(a);
}
return result;
}
示例14: Exp2
// this is already inside the loop E'--> +TE'|-TE'| e
int Exp2(int input){
int result = input;
char a;
if (!fin.eof() ){
fin.get(a);
//if( (a = fin.get()) != fin.eof()){ // just another way i could have written this. works for my notes.
if (a == '+')
result = Exp2(result + Term() );
else if(a == '-')
result = Exp2(result - Term() );
else if(a == ')')
fin.putback(a);
}
return result;
}
示例15: Term2
// T'--> *FT'| /FT'| e
int Term2(int input){
int result = input;
char a;
//if( (a = fin.get()) != fin.eof() ){ // just another way i could have written this. works for my notes.
if (!fin.eof() ){
fin.get(a);
if (a == '*')
result = Term2(result * Pwr() );
else if (a == '/')
result = Term2(result / Pwr() );
else if (a == '+' || a == '-' || a == ')') // i added the ')' to close the parenthesis . it does this because when it finds it a gets put back so it knows what operation to do
fin.putback(a);
}
return result;
}