将字符串的一部分分成更多字符串

时间:2015-02-25 17:19:11

标签: c++ fstream

我正在使用函数fscanf_s从我从.obj文件中读取的字符串中创建多个字符串。也许我在滥用它我不知道但我曾尝试使用fscanf_s(文件,“%s%s%s \ n”,& temp,& temp1,& temp2)。

这是一个例子: f 1 // 1 14 // 1 13 // 1

我想要做的是将1 // 1,14 // 1和13 // 1存储到3个单独的字符串中。

我真的不关心最快的方法,但显然会更喜欢它。

EDIT 出于某种原因ifstream>>操作员在我的项目中无法正常工作。我找到了解决自己问题的方法。

char s1[10];
char s2[10];
char s3[10];

int matches = fscanf_s(file, "%s %s %s", s1, sizeof(s1), s2, sizeof(s2), s3, sizeof(s3));

然后在字符串构造函数中使用字符。

1 个答案:

答案 0 :(得分:0)

使用c ++时,我强烈建议使用c ++函数:) 尝试这样的方法来解析你的文件:

std::ifstream file ("filename");
while (file)
{
  std::string token;
  file >> token;
  // process your token...
}

>>运算符从输入流中提取下一个非空白字符序列(看看:http://www.cplusplus.com/reference/istream/istream/operator%3E%3E/