简体   繁体   English

在程序中一起使用getline和strtok的问题

[英]Problem with using getline and strtok together in a program

In the below program , I intend to read each line in a file into a string , break down the string and display the individual words.The problem I am facing is , the program now outputs only the first line in the file. 在下面的程序中,我打算将文件中的每一行读取为一个字符串,分解该字符串并显示各个单词。我面临的问题是,该程序现在仅输出文件中的第一行。 I do not understand why this is happening ? 我不明白为什么会这样?

#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<<" ";
        }

    }

}

I'm new to C++, but I think an alternative approach could be: 我是C ++的新手,但我认为替代方法可能是:

while(getline(InputFile, store))
{
    stringstream line(store); // include <sstream>
    string token;        

    while (line >> token)
    {
        cout << "Token: " << token << endl;
    }
}

This will parse your file line-by-line and tokenise each line based on whitespace separation (so this includes more than just spaces, such as tabs, and new lines). 这将逐行解析文件,并根据空格分隔标记每一行(因此,不仅包括空格,例如制表符和换行符)。

Well, there is a problem here. 好吧,这里有个问题。 strtok() takes a null-terminated string, and the contents of a std::string are not necessarily null-terminated. strtok()接受以null终止的字符串,并且std::string的内容不一定以null终止。

You can get a null-terminated string from a std::string by calling c_str() on it, but this returns a const char* (ie, the string is not modifiable). 您可以通过在std::string调用c_str()来获取以null终止的字符串,但这会返回const char* (即,字符串不可修改)。 strtok() takes a char* and modifies the string when it is called. strtok()接受char*并在调用时修改字符串。

If you really want to use strtok() , then in my opinion the cleanest option would be to copy the characters from the std::string into a std::vector and the null-terminate the vector: 如果您真的想使用strtok() ,那么我认为最干净的选择是将字符从std::string复制到std::vector并以null终止向量:

std::string s("hello, world");
std::vector<char> v(s.begin(), s.end());
v.push_back('\0');

You can now use the contents of the vector as a null-terminated string (using &v[0] ) and pass that to strtok() . 现在,您可以将向量的内容用作以null终止的字符串(使用&v[0] )并将其传递给strtok()

If you can use Boost, I'd recommend using Boost Tokenizer . 如果可以使用Boost,建议您使用Boost Tokenizer It provides a very clean interface for tokenizing a string. 它为标记字符串提供了非常干净的接口。

What James McNellis says is correct. James McNellis所说的是正确的。

For a quick solution (though not the best), instead of 快速解决方案(虽然不是最好的),而不是

string store

use 使用

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM