C++ C++;使用多个delimter读取文件
我有下面的文本文件 text.txtC++ C++;使用多个delimter读取文件,c++,delimiter,file-handling,C++,Delimiter,File Handling,我有下面的文本文件 text.txt 1, Harry Potter, 1998, UK/trains/wizards/ 用户被要求输入一本书的名称,然后输入出版日期,并列出与该书关联的所有关键字。用户可以输入的关键字数量没有限制。此数据将在具有私有成员的类中使用,以便可以更改或删除等 我想知道我怎样才能读到文本文件,它会把《哈利波特1998》和《英国火车奇才》这样的关键词分开 下面的代码读取文件并根据一组delimter将其拆分。有没有一种方法可以让它与多个delimter一起工作,或者简单
1, Harry Potter, 1998, UK/trains/wizards/
用户被要求输入一本书的名称,然后输入出版日期,并列出与该书关联的所有关键字。用户可以输入的关键字数量没有限制。此数据将在具有私有成员的类中使用,以便可以更改或删除等
我想知道我怎样才能读到文本文件,它会把《哈利波特1998》和《英国火车奇才》这样的关键词分开
下面的代码读取文件并根据一组delimter将其拆分。有没有一种方法可以让它与多个delimter一起工作,或者简单的解决方案是为第一个数据位创建一个文件,为keyowrds创建另一个文件
std::ifstream file("test.txt");
std::string line;
if (file)
{
std::string token;
std::stringstream ss;
while (getline(file, line))
{
ss << line;
while (getline(ss, token, ','))
{
std::cout << token << std::endl;
}
ss.clear();
}
}
std::ifstream文件(“test.txt”);
std::字符串行;
如果(文件)
{
字符串标记;
std::stringstream-ss;
while(getline(文件,行))
{
不要对逗号分隔的字段使用循环。对关键字使用循环
std::string token1 = getline(ss, token1, ','); // 1
std::string token2 = getline(ss, token2, ','); // "Harry Potter"
std::string token3 = getline(ss, token3, ','); // 1998
std::vector<string> keywords;
std::string word;
while (getline(ss, word, '/'))
{
keywords.push_back(word);
}
std::string token1=getline(ss,token1,,);//1
std::string token2=getline(ss,token2,,);/“哈利波特”
std::string token3=getline(ss,token3,,);//1998
std::向量关键词;
字符串字;
while(getline(ss,word,“/”)
{
关键词:推回(word);
}
您需要根据逗号分隔符限制提取的数量。因为您的示例中只有3列,所以实际上不需要循环。简单的解决方案是完全按照您对关键字标记上的逗号所做的操作进行拆分:
std::vector parseKeywords(const std::string & keywords)
{
std::vector result;
std::stringstream keywordstrm(token);
std::string keyword;
while (getline(keywordstrm, keyword, '/'))
{
result.push_back(keyword);
}
return result;
}
您只需扩展通过任意数量字段的while
循环,并使用内部循环中的第二个分隔符进一步分解每个字段:
while (getline(file, line)) {
ss << line;
while (getline(ss, token, ',')) {
std::stringstream ss2(token); // parse now the field that was read
while (getline(ss2, tok2, '/')) // and decompose it again
std::cout << tok2 << " + ";
std::cout << std::endl;
}
ss.clear();
}
while(getline(文件,行)){
ss假设您知道文本文件的确切格式,可能会更好,但对于您不知道的更一般的情况,您可以尝试此解决方案,它适用于任何一组delimeters,并且与getline一样工作
std::istream& getline2(std::istream& stream, std::string& s, const std::string& delimeters){
s.clear(); char c;
while(stream.read(&c, 1)){
if(delimeters.find(c) != std::string::npos)
break;
s += c;
}
return stream;
}
用法示例:
while (getline2(ss, token, ",/\."))
{
std::cout << token << std::endl;
}
while(getline2(ss,token,“,/\”)
{
在这种情况下,可以使用strtok
std::ifstream文件(“test.txt”);
std::字符串行;
如果(文件)
{
字符串标记;
std::stringstream-ss;
while(getline(文件,行))
{
char*pch;
pch=strtok(str.c_str(),“,.-”;
while(pch!=NULL)
{
std::cout返回const char*
。需要char*
,因为它修改了提供的c样式字符串。您需要strdup
或类似文件将字符串复制到非常量缓冲区中。
`std::ifstream file("test.txt");
std::string line;
if (file)
{
std::string token;
std::stringstream ss;
while (getline(file, line))
{
char * pch;
pch = strtok (str.c_str()," ,.-");
while (pch != NULL)
{
std::cout<<pch<<std::endl;
pch = strtok (NULL, " ,.-");
}
ss.clear();
}
}`