C++ 使用strtok函数时的问题
大家早上好,:) < >我编写了一个C++代码,从TXT文件读取信息。它获取第一行中的信息,并将其保存为字符串,然后我想处理此信息。我想读这个字符串,当它找到一个“|”字符时,它必须跳转到新行。这很简单,但我在执行时遇到了问题,我一直在努力寻找问题,但一直没有成功(我附上代码 提前感谢你的帮助C++ 使用strtok函数时的问题,c++,C++,大家早上好,:) < >我编写了一个C++代码,从TXT文件读取信息。它获取第一行中的信息,并将其保存为字符串,然后我想处理此信息。我想读这个字符串,当它找到一个“|”字符时,它必须跳转到新行。这很简单,但我在执行时遇到了问题,我一直在努力寻找问题,但一直没有成功(我附上代码 提前感谢你的帮助 #include <string> #include <fstream> #include <vector> #include <iostream> us
#include <string>
#include <fstream>
#include <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;
}
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
ifstream ifs(“C:\\a\\text.txt”);
字符串温度;
getline(ifs、temp);
CUT< P>你会发现一些方法来实现这一点。当你使用C++时,它通常不是诉诸C样式函数的方法。关于你的代码,试着给我们精确的错误。它帮助……/P>
我发现您的代码中存在很多问题。以下是一个工作示例:
/*strtok示例*/
#包括
#包括
int main()
{
char str[]=“-这是一个示例字符串。”;
char*pch;
printf(“将字符串\%s\”拆分为标记:\n”,str);
pch=strtok(str,“,.-”);
while(pch!=NULL)
{
printf(“%s\n”,pch);
pch=strtok(空,,.-”;
}
返回0;
}
正如文档中所述,您需要给strtok一个非常量c字符串,因为它将修改它以处理令牌
my2c尝试更改此行
pch = strtok (temp,"|");
到
strtok接受char*(c样式字符串)而不是::std::string(c++字符串)
更新2
我错了,我很久没用strtok了。
好啊
试试这个:
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;
strtok使用char*,而不是string*。这可能就是您遇到问题的原因
因为你用C++实现这个,我建议你使用字符串函数而不是Strtok:< /P>
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;
}
intmain()
{
ifstream ifs(“C:\\a\\text.txt”);
字符串温度;
getline(ifs、temp);
cout有很多方法可以标记std::string。这里有一种方法;我选择它主要是因为它简单且自包含:
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;
}
intmain(){
使用名称空间std;
ifstream ifs(“C:/a/text.txt”);
向量位;//如果要保存以进行处理
for(字符串临时;getline(如果,临时,“|”);){
位。推回(温度);
你会遇到什么样的问题吗?我在编译时遇到问题。在程序的第二部分,当使用strok函数时,我似乎有一些错误:在第24行:无法将参数1'的std::string'转换为char*,
char*strok(char*,const char*)'在第29行中,无法在赋值中将char*”转换为std::string*,但我真的不理解这个问题,也不知道如何解决它…:很可能它无法编译,因为他使用的std::string函数是为char*
设计的,所以我如何重写我的代码,以便在编译?请不要从那些告诉你将system(“pause”)
放在主要函数末尾的教程/书籍中学习。我已经在前面的评论中附上了错误。那么,我该如何重写代码以使其正常工作呢???@thomas:我添加了一个示例。它应该会成功:)但更喜欢STL方式或boost方式:)temp.c_str()是char const*,不是char*。它会犯以下错误:从const char*”到char*”的转换无效更新仍然不起作用,当然,因为您没有将temp复制到s中。更新中的代码分配的字符太少,不会将数据从temp复制到数组中,并且应该避免手动释放资源(例如删除)在C++中,我总是使用::STD::vector在发布中,但是作者并不真正理解C风格字符串和::STD::字符串,所以我发布了最简单的决定。是的,我忘记了MimCPy,但是在帖子编辑后发现了这个错误。现在用你的想法解决问题。继续我的代码,现在我想识别和将从我的文件中读取并用“|”分隔的每个不同元素分配给一个变量。但是当将其定义为temp.substr(0,tokenPos)时,我在编译时遇到问题。我附加了我的代码:#include#include#include#include#include使用命名空间std;int main(){ifstream ifs(“C:\\a\\text.txt”);int-id;int-type;int列;字符串temp;getline(ifs,temp);coutif(i=1){type=temp.substr(0,tokenPos);}if(i=2){columns=temp.substr(0,tokenPos);}i++;}可能无法将字符串直接分配给int。如果希望从字符串中获得int,请使用type=atoi(temp.substr(0,tokenPos.c_str());列也可以使用type=atoi。此外,comparison运算符为==,因此请使用If(i==1),而不是If(i=1)。别忘了在while循环结束之前执行++i;。Greaaaaaat!!!它工作得很好!!再次感谢!!像你这样的人真的激励我继续编程!:)
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;
}
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;
}
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;
}