How to read a file using getline?

Can you please help me to read a file and pass it through getNextLex? I havw an example below this program which shows what i am trying to do.


#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

const int NOMORETOKENS = 0;
const int IDENTIFIER = 1;
const int DIGITSTRING = 2;
const int LPAR = 3;
const int RPAR = 4;
const int ADDOP = 5;
const int MULOP = 6;


//------------------------------------------------------------------------------
// determine if c is a letter (upper/lower case)
bool isLetter(char c)
{
return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z');
}

//------------------------------------------------------------------------------
// determine if c is a digit character
bool isDigit(char c)
{
return '0' <= c && c <= '9';
}

//------------------------------------------------------------------------------
// determine if c is a multiplication operator
bool isMulop(char c)
{
return (c=='*' || c=='/');
}

//------------------------------------------------------------------------------
// determine if c is an addition operator
bool isAddop(char c)
{
return c=='+' || c=='-';
}

int getNextLex(string &s, string &t)
{

int i=0;
while(i<s.length()&& s[i]==' ')
{
i++;
}
if(i==s.length())
{
return NOMORETOKENS;
}
while(i<s.length())
{

if(s[i]=='(')
{

t=t+s[i];
s=s.substr(i+1);
return LPAR ;
}

else if(isLetter(s[i]))
{
while(isLetter(s[i]))
{

t=t+s[i];
i++;
}
s=s.substr(i);
return IDENTIFIER ;
}
else if(isDigit(s[i]))
{
while(isDigit(s[i]))
{

t=t+s[i];
i++;

}
s=s.substr(i);
return DIGITSTRING;
}

else if( isMulop(s[i]))
{

t=t+s[i];

s=s.substr(i+1);
return MULOP;
}
else if(isAddop(s[i]) )
{

t=t+s[i];

s=s.substr(i+1);
return ADDOP;
}
else if(s[i]==')')
{
t=t+s[i];
i++;

s=s.substr(i);
return RPAR;

}

i++;
}


// s=s.substr(i);
//return 0;

} // end of getNextLex()





//==============================================================================
// return the name of the lexical unit whose code is p
string lexName(int p)
{
string names[] = {"NoTokens", "Identifer", "DigitString", "LeftParent", "RightParent",
"AddOp", "MulOp"};
return names[p];
}



int main(int argc, char *argv[])
{
string file;
cout<<"Name the file to be processed : ";
cin>>file;
string filename = file;
ifstream infile;
infile.open(filename.c_str()); // open file
if(infile)
{
string s="";
while(infile)
{
getline(infile,s);
cout<<s<<endl;
}
while(s.length()>0)
{
string t= "";
int ln= getNextLex(s,t);
cout<<"\t"<<t<<"\t "<<lexName(ln)<<"("<<ln<<")"<<endl;
}


system("PAUSE");
return EXIT_SUCCESS;

}
else
{
cout << "The file name you gave does not exist"<<endl;
}
infile.close(); // file close


system("PAUSE");
return EXIT_SUCCESS;
}




________________________________________________________________________________

file.txt


% Here are some easy expressions
a
aaa+bb
a+bba+b+19
a*b+c
a*b*c*19
How * now + brown * cow


% Here are some easy invalid expressions
a+
a + b c
a+ 31 * 5+
a*b*5 13*a






Last edited on
1
2
3
4
5
6
7
infile.open(filename.c_str()); // open file
if(infile) {
  string s="";
  while(getline(infile, s)) {
    cout<<s<<endl;
  }
}   
Topic archived. No new replies allowed.