#include <iostream>
#include <string>
#include <fstream>
usingnamespace std;
int main ()
{
ifstream infile;
infile.open("test.txt");
string line;
char *str;
char *temp;
cout << "Splitting the line into tokens\n";
while (!infile.eof())
{
getline(infile, line);
str = newchar[sizeof line];
for (unsignedint i=0; i<= line.length(); i++)
str[i] = line[i];
temp = strtok (str,"\t");
while (temp!=NULL)
{
cout << endl << temp;
temp = strtok (NULL, "\t");
}
cout << endl;
}
cout << endl << endl;
return 0;
delete [] str;
}
Here is a sample input that I have:(each char is separated by a \t)
1 1 0 0 A
Now my code breaks it up into tokens when it looks like, but what I want it to do is to break it up into and recognize blank, or empty. Something like this:
1 1 A
My code will print this for the first sample:
1
1
0
0
A
And this for the second sample:
1
1
A
What I want it to do is to print 0s for any blank/empty positions:
I agree. Please don't use strtok() in C++ programs. (The function is evil to begin with.)
There was a recent Article on it: http://www.cplusplus.com/forum/articles/4860/
(Be aware that my example in this article does not account for empty fields --the same as strtok().)
You can also use getline() and a stringstream:
(This example does account for empty fields.)