我需要将字符串转换为char*以便在strtok_s中使用,并且无法弄明白.c_str()转换为const char*,它是不兼容的.
此外,如果有人可以向我解释为什么第二个strtok_s函数(在循环内)是必要的,这将是一个很大的帮助.为什么我需要明确地推进令牌而不是例如它所处的while循环,它隐式地连续地获取文件的每一行.
while( getline(myFile, line) ) { // Only one line anyway. . . is there a better way?
    char * con = line.c_str();
    token = strtok_s( con, "#", &next_token);
    while ((token != NULL))
    {
        printf( " %s\n", token );
        token = strtok_s( NULL, "#", &next_token);
    }
}
相关问题.
我有一个std :: wstring变量,其中包含文本,我需要用分隔符将其拆分。我该怎么办?我不会使用会产生一些警告的Boost。谢谢
编辑1, 这是一个示例文本:
你好你好吗?
这是代码:
typedef boost::tokenizer<boost::char_separator<wchar_t>, std::wstring::const_iterator, std::wstring> Tok;
boost::char_separator<wchar_t> sep;
Tok tok(this->m_inputText, sep);
for(Tok::iterator tok_iter = tok.begin(); tok_iter != tok.end(); ++tok_iter)
{
    cout << *tok_iter;
}
结果是:
我不明白为什么最后一个字符总是被分割成另一个标记...
说我有三架C风格的字符串,char buf_1[1024],char buf_2[1024],和char buf_3[1024].我想对它们进行标记化,并使用所有三个中的第一个标记执行操作,然后对所有三个中的第二个标记执行相同操作等.显然,strtok每次我想要一个新标记时,我可以从头开始调用并循环它们.或者,预先处理所有令牌,将它们分成三个阵列并从那里开始,但我想要一个更清洁的解决方案,如果有的话.
我需要一些帮助,从std :: string中获取所有整数,并将每个整数转换为int变量.
字符串示例:
<blah> hi 153 67 216
我希望程序忽略"blah"和"hi"并将每个整数存储到一个int变量中.所以它就像是:
a = 153
b = 67
c = 216
然后我可以自由地分别打印每个像:
printf("First int: %d", a);
printf("Second int: %d", b);
printf("Third int: %d", c);
谢谢!
当我尝试从字符串中解析空白分隔的double值时,我发现了这种奇怪的行为,即以循环方式读出字符串.
这是程序:
stringstream ss;
string s("1 2 3 4");
double t;
list<double> lis;
for(int j=0; j!=10; ++j){
    ss << s;
    ss >> t;
    lis.push_back(t);
}
for(auto e : lis){
    cout << e << " ";
}
这里输出:
1 2 3 41 2 3 41 2 3 41
如果我追加尾随空间,s= "1 2 3 4 ";我得到
1 2 3 4 1 2 3 4 1 2 
现在问题:
1)如果我不知道字符串s中有多少条目,我如何读入列表中的所有条目?
2)operator<<我实际上是在打电话ss << s;?是否指定循环阅读?
3)我可以更好地解析吗?  
谢谢!
这是固定代码(感谢timrau):
// …我有两个列的CSV文件:名称,年龄
为了阅读和存储信息,我做到了这一点
struct person
{
    string name;
    int age;
}
person record[10];
ifstream read("....file.csv");
但是,当我这样做的时候
read >> record[0].name;
read.get();
read >> record[0].age;
阅读>>名字给了我整行而不仅仅是名字.我怎么可能避免这个问题,以便我可以读取整数到年龄?
谢谢!
我正在努力调整这个答案
到我目前的字符串问题,涉及从文件读取到eof.
来自这个源文件:
Fix grammatical or spelling errors
Clarify meaning without changing it
Correct minor mistakes
我想创建一个包含所有标记化单词的向量.示例:vector<string> allTheText[0] should be "Fix"
我没有说明目的,istream_iterator<std::string> end;但我包括原因,这是在原始海报的答案.
到目前为止,我有这个非工作代码:
vector<string> allTheText;
          stringstream strstr;
          istream_iterator<std::string> end;
          istream_iterator<std::string> it(strstr);
          while (!streamOfText.eof()){
                getline (streamOfText, readTextLine);
                cout<<readTextLine<<endl;
                stringstream strstr(readTextLine);
                // how should I initialize the iterators it and end here?
                }
编辑:
我将代码更改为
          vector<string> allTheText;
          stringstream strstr;
          istream_iterator<std::string> end;
          istream_iterator<std::string> it(strstr);
          while (getline(streamOfText, readTextLine)) {
               cout << readTextLine << endl;
        vector<string> vec((istream_iterator<string>(streamOfText)), istream_iterator<string>()); // generates …我有一个像这样的字符串:
aaa bbb
在弦的第二部分之前有一个空格.我的目标是只解析第一部分,所以aaa.空间结束后的一切都在外面.我怎么能用C++做到这一点?
我想解析一个逐行描述一组数据的文件.每个数据由3或4个参数组成:int int float(optional)string.
我打开文件ifstream inFile并在while循环中使用它
while (inFile) {
    string line;
    getline(inFile,line);
    istringstream iss(line);
    char strInput[256];
    iss >> strInput;
    int i = atoi(strInput);
    iss >> strInput;
    int j = atoi(strInput);
    iss >> strInput;
    float k = atoi(strInput);
    iss >> strInput;
    cout << i << j << k << strInput << endl;*/
}
问题是最后一个参数是可选的,所以当它不存在时我可能会遇到错误.如何提前检查每个数据的参数数量?
此外,
    string line;
    getline(inFile,line);
    istringstream iss(line);
看起来有点红,我怎么能简单地说呢?
我是c++新手,基本上属于PHP。所以我试图编写一个程序只是为了练习,对数组进行排序。我已经成功创建了具有静态数组值的程序,即
// sort algorithm example
#include <iostream>     // std::cout
#include <algorithm>    // std::sort
#include <vector>       // std::vector
bool myfunction (int i,int j) { return (i<j); }
struct myclass { bool operator() (int i,int j) { return (i<j);} } myobject;
int main () {
   int myints[] = {55,82,12,450,69,80,93,33};
  std::vector<int> myvector (myints, myints+8);               
  // using default comparison (operator <):
  std::sort (myvector.begin(), myvector.begin()+4);           
  // using function as comp
  std::sort (myvector.begin()+4, myvector.end(), myfunction); 
  // using object as comp
  std::sort (myvector.begin(), myvector.end(), myobject);     
  // print …