分割字符串
Dividing strings
所以我正在编写自己的代码来加载obj文件。但我正试图以这种格式划分字符串:(2个例子)
f 1/2/3 4/5/6 7/8/9
f 13/45/76 445/7776/5566 677/7/45
3组3个数字,除斜杠之间外,每个位置后面都有空格。到目前为止,我有这个代码。
此时,程序已经去掉了'f',但是字符串前面有一个空格,所以它就像" 1/2/3 4/5/6 7/8/9"
第二组("g2")是唯一不工作的组。它返回"1/2/3/7/8"
buffer是我要分割的字符串。
// Divide into groups
// Create groups of 1/1/1, 2/2/1, 3/3/1 Ex
// At this point the buffer = SPACEHEREx/y/z u/v/w xn/yn/zn
string g1 = buffer.substr(1, buffer.find(' ', 1) - 1); // Pos 1 - First space
string g2 = buffer.substr(buffer.find(' ', 1) + 1, buffer.find(' ', buffer.find(' ', 1) + 1) - 1); // First space - Second space
string g3 = buffer.substr(buffer.find(' ', buffer.find(' ', 1) + 1) + 1, buffer.size()); // Second space - End
我还没有资格评论帖子,抱歉。
我绝对推荐把这些分成不同的行。甚至取缓冲。找到部件并将其设置为适当的位置名称。这样会更容易调试,也更容易阅读。
如果一些额外的int
太多,然后在调试完成后将它们合并回去。
你可以尝试的另一件事是在空格上分割缓冲行,只保留"x/y/z",调用"被'/'分割"的函数,然后将这些存储到适当的变量中。我认为。obj文件的"f顶点/纹理/正常顶点/纹理/正常...."。
这可能对你有帮助:
http://www.rastertek.com/dx11tut08.html它描述了如何加载和呈现一个。obj文件。
下面是如何从obj文件中读取行(包括您尝试读取的面("f"))的代码片段:
// Initialize the indexes.
vertexIndex = 0;
texcoordIndex = 0;
normalIndex = 0;
faceIndex = 0;
// Open the file.
fin.open(filename);
// Check if it was successful in opening the file.
if(fin.fail() == true)
{
return false;
}
// Read in the vertices, texture coordinates, and normals into the data structures.
// Important: Also convert to left hand coordinate system since Maya uses right hand coordinate system.
fin.get(input);
while(!fin.eof())
{
if(input == 'v')
{
fin.get(input);
// Read in the vertices.
if(input == ' ')
{
fin >> vertices[vertexIndex].x >> vertices[vertexIndex].y >> vertices[vertexIndex].z;
// Invert the Z vertex to change to left hand system.
vertices[vertexIndex].z = vertices[vertexIndex].z * -1.0f;
vertexIndex++;
}
// Read in the texture uv coordinates.
if(input == 't')
{
fin >> texcoords[texcoordIndex].x >> texcoords[texcoordIndex].y;
// Invert the V texture coordinates to left hand system.
texcoords[texcoordIndex].y = 1.0f - texcoords[texcoordIndex].y;
texcoordIndex++;
}
// Read in the normals.
if(input == 'n')
{
fin >> normals[normalIndex].x >> normals[normalIndex].y >> normals[normalIndex].z;
// Invert the Z normal to change to left hand system.
normals[normalIndex].z = normals[normalIndex].z * -1.0f;
normalIndex++;
}
}
// Read in the faces.
if(input == 'f')
{
fin.get(input);
if(input == ' ')
{
// Read the face data in backwards to convert it to a left hand system from right hand system.
fin >> faces[faceIndex].vIndex3 >> input2 >> faces[faceIndex].tIndex3 >> input2 >> faces[faceIndex].nIndex3
>> faces[faceIndex].vIndex2 >> input2 >> faces[faceIndex].tIndex2 >> input2 >> faces[faceIndex].nIndex2
>> faces[faceIndex].vIndex1 >> input2 >> faces[faceIndex].tIndex1 >> input2 >> faces[faceIndex].nIndex1;
faceIndex++;
}
}
// Read in the remainder of the line.
while(input != 'n')
{
fin.get(input);
}
// Start reading the beginning of the next line.
fin.get(input);
}
// Close the file.
fin.close();
EDIT:要解决评论中的问题,这里有一个可能的解决方案:
std::string buffer = " 1/2/3 4/5/6 7/8/9";
string g1 = buffer.substr(1, buffer.find(' ', 1) - 1); // Pos 1 - First space
string g2 = buffer.substr(buffer.find(' ', 1) + 1, g1.length()); // First space - Second space
string g3 = buffer.substr(buffer.find(' ', buffer.find(' ', 1) + 1) + 1, g1.length()); // Second space - End
工作示例:http://ideone.com/07aVQO
问题是substr()
的第二个参数。它应该是长度,而不是输入字符串中的位置。
相关文章:
- 如何在C++中从字符串中分割字符
- 由cin中的字符串中未捕获空白引起的分割错误
- 按分度分割字符串
- 分割故障:核心倾倒C 矢量对字符串:
- C - 返回字符串时的分割故障
- 在结构中使用字符串时的分割故障
- 为什么当从递归函数返回字符串到C 中的Main时,我会得到分割错误
- 在字符串中计数字符出现时,C 分割故障
- 简单的字符串分配导致分割故障
- 试图进入字符串时的分割故障
- 分割故障添加了两个表示字符串的大数字
- 我应该使用哪个函数来分割字符串
- 如何使用STL分割字符串
- C++如何使用多个分隔符分割字符串流
- 使用循环将字符串分割为特定长度的子单元
- 将字符串分割为字符串向量时出现无效转换错误
- 将一行(字符串)分割成更多的字符串
- c++字符串分割和操作
- 字符串分割错误
- 将字符串分割或修剪成一行