[英]reading from istringstream using getline and maybe strtok somehow?
[英]Problem with using getline and strtok together in a program
在下面的程序中,我打算将文件中的每一行读取为一个字符串,分解该字符串并显示各个单词。我面临的问题是,该程序现在仅输出文件中的第一行。 我不明白为什么会这样?
#include<iostream>
#include<string>
#include<fstream>
#include<cstdio>
using namespace std;
int main()
{
ifstream InputFile("hello.txt") ;
string store ;
char * token;
while(getline(InputFile,store))
{
cout<<as<<endl;
token = strtok(&store[0]," ");
cout<<token;
while(token!=NULL)
{
token = strtok(NULL," ");
cout<<token<<" ";
}
}
}
我是C ++的新手,但我认为替代方法可能是:
while(getline(InputFile, store))
{
stringstream line(store); // include <sstream>
string token;
while (line >> token)
{
cout << "Token: " << token << endl;
}
}
这将逐行解析文件,并根据空格分隔标记每一行(因此,不仅包括空格,例如制表符和换行符)。
好吧,这里有个问题。 strtok()
接受以null终止的字符串,并且std::string
的内容不一定以null终止。
您可以通过在std::string
调用c_str()
来获取以null终止的字符串,但这会返回const char*
(即,字符串不可修改)。 strtok()
接受char*
并在调用时修改字符串。
如果您真的想使用strtok()
,那么我认为最干净的选择是将字符从std::string
复制到std::vector
并以null终止向量:
std::string s("hello, world");
std::vector<char> v(s.begin(), s.end());
v.push_back('\0');
现在,您可以将向量的内容用作以null终止的字符串(使用&v[0]
)并将其传递给strtok()
。
如果可以使用Boost,建议您使用Boost Tokenizer 。 它为标记字符串提供了非常干净的接口。
James McNellis所说的是正确的。
快速解决方案(虽然不是最好的),而不是
string store
使用
const int MAX_SIZE_LINE = 1024; //or whatever value you consider safest in your context.
char store[MAX_SIZE_LINE];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.