cplusplus
.com
TUTORIALS
REFERENCE
ARTICLES
FORUM
C++
Tutorials
Reference
Articles
Forum
Forum
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Lounge
Jobs
Forum
General C++ Programming
Easy way to Tokenize a file
Easy way to Tokenize a file
Mar 31, 2013 at 7:41am UTC
ASCE
(1)
Input file
int a;
int b;
a=2;
b=4;
print a;
Here is my code
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
void main()
{
string data="",super="",inp[12];
fstream f;
size_t a;
int i=0;
bool check=true;
f.open("inp.txt",ios::in);
while(f.good())
{
check=true;
getline(f,data);
while(check)
{
a=data.find(' ');
if(a!=string::npos) /*1st token*/
{
super=data.substr(0,a);
}
else
{
super=data.substr(0,data.size()); /* last token end of inner while */
check=false;
}
if(super!="")
{
inp[i++]=super;/* Token insertion*/
}
data=data.substr(a+1,data.size()); /* 2nd substring the original string */
}
}
f.close();
for(int j=0;j<i;j++)
cout<<inp[j]<<endl;
}
Last edited on
Mar 31, 2013 at 7:58am UTC
Topic archived. No new replies allowed.