把一个字符串的一部分分成更多的字符串
separate part of a string into more strings
我使用函数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/)
相关文章:
- C++:如何使函数只返回作为列表一部分的字符串
- 如何将字符串的一部分转换为整数
- 如何有效地将一个大std::字符串的一部分转换为float
- 需要从字符串数组的一部分访问元素
- 当 NUL 字符被定义为字符串的一部分时,为什么 strlen() 不计算终止 NUL 字符的字节?
- 如何正确实现 std::all_of 函数来验证字符串的一部分?
- boost::async_write 跳过字符串的一部分
- 如何在不复制的情况下比较字符串的一部分?
- 我如何在QT中获得字符串的一部分
- 如何在C++中比较数组中字符串的一部分的字符串
- 如何在 c++ 中拆分字符串的一部分
- 字符数组仅占用字符串的一部分
- 如何用双C 替换字符串的一部分
- 如何在字符串中选择句子的一部分 (C++)
- 如何拆分 QString 并将一部分字符串保存在浮点数的向量中
- 为什么C 正则只能找到JavaScript Regex在字符串中找到的一部分
- 字符串操纵和忽略字符串的一部分
- 在C 中更新字符串的一部分
- 有条件地确定字符串是否是数组的一部分
- 尝试在 Cocos2d-X C++中设置一个带有整数作为其字符串一部分的 CCLabelTTF