如何将文件中的三个值加载到char*数组中(C语言)?

huangapple go评论81阅读模式
英文:

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 &quot;v &quot; 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] == &#39;v&#39;) {
		int currCoord = 0;
		char* vCoords[3] = { &quot;&quot;, &quot;&quot;, &quot;&quot;};

		for (int i = 2; i &lt; sizeof(buffer); i++) {
			if (buffer[i] == &#39; &#39;) {
				currCoord++;
				continue;
			}

			strcat_s(vCoords[currCoord], sizeof(&amp;buffer[i]), &amp;buffer[i]);
		}
	}
}

printf(&quot;%s\n&quot;, 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&#39;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] = { &quot;&quot;, &quot;&quot;, &quot;&quot;};

You've got yourself a nice array of three pointers to string literals.

strcat_s(vCoords[currCoord], sizeof(&amp;buffer[i]), &amp;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(&amp;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, &quot; &quot;);
for (int token_idx = 0; token_idx &lt; 3 &amp;&amp; token != NULL, ++token_idx)
{
   strcpy(vCoords[token_idx], token);
   token = strtok(NULL, &quot; &quot;);
}

Of course you want to do something with vCoords, otherwise you could just replace the function with an empty pair of braces.

huangapple
  • 本文由 发表于 2023年6月9日 05:48:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76435909.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定