英文:
How to load 3 values from a file into a char* array (C)?
问题
我尝试实现一个函数,它打开一个obj文件并循环遍历每一行的第二个字符开始的每个字符,因为第一个和第二个字符只是代表顶点的“v”。不知何故,我得到了一个访问冲突错误。以下是代码:
```cpp
bool LoadFromObjectFile(char* filepath) {
FILE* pF;
fopen_s(&pF, filepath, "r");
if (pF == NULL)
return false;
char buffer[128];
while (fgets(buffer, sizeof(buffer), pF) != NULL) {
if (buffer[0] == 'v') {
int currCoord = 0;
char* vCoords[3] = { "", "", "" };
for (int i = 2; i < sizeof(buffer); i++) {
if (buffer[i] == ' ') {
currCoord++;
continue;
}
strcat_s(vCoords[currCoord], sizeof(buffer[i]), &buffer[i]);
}
}
}
printf("%s\n", buffer);
fclose(pF);
return true;
}
我尝试使用strcat函数将当前字符添加到vCoords的一个中。我以为它会立即起作用,但我得到了一个错误,我不知道为什么。
<details>
<summary>英文:</summary>
I tried implementing a Function which opens a obj file and loops over every character in one line starting from the second character, because the first and the second character are just "v " which stands for vertex. Somehow I get an acces violation error. Here is the code:
bool LoadFromObjectFile(char* filepath) {
FILE* pF;
fopen_s(&pF, filepath, "r");
if (pF == NULL)
return false;
char buffer[128];
while (fgets(buffer, sizeof(buffer), pF) != NULL) {
if (buffer[0] == 'v') {
int currCoord = 0;
char* vCoords[3] = { "", "", ""};
for (int i = 2; i < sizeof(buffer); i++) {
if (buffer[i] == ' ') {
currCoord++;
continue;
}
strcat_s(vCoords[currCoord], sizeof(&buffer[i]), &buffer[i]);
}
}
}
printf("%s\n", buffer);
fclose(pF);
return true;
}
I tried to use the strcat function to add the current character to one of the vCoords. I thought it's going to work immediately but I get an error and I have no idea why.
</details>
# 答案1
**得分**: 1
```c
char* vCoords[3] = { "", "", "" };
你有一个包含三个指向字符串文字的指针的数组。
strcat_s(vCoords[currCoord], sizeof(&buffer[i]), &buffer[i]);
你在这里尝试修改一个字符串文字。这是不允许的。即使你可以修改字符串文字,你也在使用`sizeof(&buffer[i])`,这在多个层面上都是完全错误的,并且你的字符串文字足够大以包含一个空字符串和什么都没有。
实际上并不清楚你试图做什么。循环似乎没有任何可见的效果。你只是在循环之后打印文件的最后一行。这是你的函数所做的一切。
如果你像我猜测的那样,想从每行中提取三个以空格分隔的标记,你可以使用`strtok`函数以大致以下方式进行(注意,未经测试的代码):
char vCoords[3][128];
char* start = buffer + 2;
char* token = strtok(buffer, " ");
for (int token_idx = 0; token_idx < 3 && token != NULL; ++token_idx)
{
strcpy(vCoords[token_idx], token);
token = strtok(NULL, " ");
}
当然,你想要对`vCoords`进行一些操作,否则你可以将该函数替换为一对空括号。
英文:
char* vCoords[3] = { "", "", ""};
You've got yourself a nice array of three pointers to string literals.
strcat_s(vCoords[currCoord], sizeof(&buffer[i]), &buffer[i]);
You are trying to modify a string literal here. This is not allowed. Even if you could modify a string literal, which you can't, you are using sizeof(&buffer[i])
which is totally wrong at several levels, and your string literals are big enough to contain an empty string and nothing else.
It isn't actually clear at all what you are trying to do. The loop doesn't seem to have any visible effect. You just print the last line of the file after the loop. That's all your function does.
If you, as my guess is, want to pull three space-separated tokens out of each line, you do this with the strtok
function, in this approximate fashion (caveat, untested code):
char vCoords[3][128];
char* start = buffer + 2;
char* token = strtok(buffer, " ");
for (int token_idx = 0; token_idx < 3 && token != NULL, ++token_idx)
{
strcpy(vCoords[token_idx], token);
token = strtok(NULL, " ");
}
Of course you want to do something with vCoords
, otherwise you could just replace the function with an empty pair of braces.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论