把一个字符串的一部分分成更多的字符串

separate part of a string into more strings

本文关键字:字符串 一部分 一个      更新时间:2023-10-16

我使用函数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个单独的字符串中。

我真的不在乎用最快的方法来做,但显然更可取。

编辑由于某种原因,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));

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

当使用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/)