I am reading a line of text from a file and need to split it into tokens.
This is a test and test number is: test(001)
I need the tokens to be
This
is
a
test
and
test
number
is
:
test
(
001
)
How do i split a string into tokens
here is my code so far
#include <fstream>
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <stdio.h>
using namespace std;
vector <string> SplitString (string line)
{
//this is where the split needs to occur
}
bool ispunct (char aCharacter, string delimiters)
{
int numDelimiters = delimiters.length ();
for (int i = 0; i < numDelimiters; i++)
{
if (aCharacter == delimiters[i])
return true;
}
return false;
}
int main()
{
int i;
int a=0;
int c;
char ch;
string line;
vector <string> tokens;
ifstream myFile("scan.cm");
if (! myFile)
{
cout << "Error opening output fle" << endl;
return -1;
}
while( getline( myFile, line ) )
{
a++;
vector <string> newTokens = SplitString (line);
int numNewTokens = newTokens.size();
for (int i = 0; i < numNewTokens; i++)
{
tokens.push_back (newTokens[i]);
}
cout << "line " << a << ": " << endl;
}
myFile.close();
return 0;
}