Problems when using strtok function
Good morning to everybody, :)
I have writen a code in C++ that reads information from a txt file. It takes the information in the first row saving it in a string and then I want to work with this info. I want to read this string and when it finds a "|" character it must jump to a new line. Its something quite easy but I'm having problems when executing and I've been trying to find the problem for hours and I haven't suceed. :( I attached the code.
Thanks in advance for your help.
#include <string>
#include <fstream>
#include <开发者_如何学Python;vector>
#include <iostream>
using namespace std;
int main()
{
ifstream ifs( "C:\\a\\text.txt" );
string temp;
getline( ifs, temp );
cout<<temp<<endl;
string * pch;
pch = strtok (temp,"|");
while (pch != NULL)
{
printf ("%s\n",pch);
pch = strtok (NULL, "|");
}
system("pause");
return 0;
}
strtok works with char*, not string*. That's probably why you're experiencing issues.
Since you're implementing this with c++, I suggest you use the string functions instead of strtok:
int main()
{
ifstream ifs( "C:\\a\\text.txt" );
string temp;
getline( ifs, temp );
cout<<temp<<endl;
size_t tokenPos = temp.find("|");
while (tokenPos != string::npos)
{
cout << temp.substr(0, tokenPos) << endl;
temp.erase(0, tokenPos+1);
tokenPos = temp.find("|");
}
system("pause");
return 0;
}
To store your text in the values you described in your comment, you'd do the following:
int main()
{
ifstream ifs( "C:\\a\\text.txt" );
int id;
int type;
int columns;
string temp;
getline( ifs, temp );
cout<<temp<<endl;
size_t tokenPos = temp.find("|");
while (tokenPos != string::npos)
{
int i=0;
tokenPos = temp.find("|");
cout << temp.substr(0, tokenPos) << endl;
if(i==0)
{
id = atoi(temp.substr(0, tokenPos).c_str());
}
else if(i==1)
{
type = atoi(temp.substr(0, tokenPos).c_str());
}
else if(i==2)
{
columns = atoi(temp.substr(0, tokenPos).c_str());
}
++i;
temp.erase(0, tokenPos+1);
}
cout << "ID: " << id << ", Type: " << type << ", Columns: " << columns << endl;
system("pause");
return 0;
}
There are many ways to tokenize a std::string. Here is one way; I chose this primarily because it's simple and self-contained:
int main() {
using namespace std;
ifstream ifs("C:/a/text.txt");
vector<string> bits; // if you want to save to process
for (string temp; getline(ifs, temp, '|');) {
bits.push_back(temp);
cout << temp << '\n'; // if you want to output each as processed
}
return 0;
}
You will find some ways to do that in that question. As you use C++, it is usually not the way to resort to C style functions. Concerning your code, try to give us the exact error. It helps ...
I see lots of problems in your code. Here is a working example from C++ reference :
/* strtok example */
#include <stdio.h>
#include <string.h>
int main ()
{
char str[] ="- This, a sample string.";
char * pch;
printf ("Splitting string \"%s\" into tokens:\n",str);
pch = strtok (str," ,.-");
while (pch != NULL)
{
printf ("%s\n",pch);
pch = strtok (NULL, " ,.-");
}
return 0;
}
As stated in the doc, you need to give strtok a non-const c-string as it will modify it in order to process tokens.
my2c
Try to change this line
pch = strtok (temp,"|");
to
pch = strtok (temp.c_str(),"|");
strtok takes char* (c-style string) not ::std::string (c++ string).
UPDATE2 My mistake. I haven't used strtok for so long. Ok. Try this one:
char* s = new char[temp.size() + 1];
memcpy(s, temp.c_str(), temp.size() + 1); // copy c-style string with '\0' at the end
const char* pch = strtok(s, "|");
...
delete[] s;
精彩评论