图像下载到文件系统,使用生成的名称。

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

Image Download to File System Using Generated Name

问题

I can help you with the translation:

我正在使用stb库来编辑一张图片,然后尝试将这张图片保存到磁盘上。图片处理过程正常运行,但生成文件名的过程失败,生成了名为"mnn"和"mfn"的文件。这些文件需要我在末尾添加".png"扩展名,然后才能查看图片(顺便说一句,它们看起来很棒)。

以下是我的代码:

for (unsigned int rep = 0; rep < 3; rep++) {
    // 执行图像变换
    create_square(50, 75, 100, 300, rand() % 255, rand() % 255, rand() % 255);

    char *downloadName = (char *)malloc(sizeof(("modified_image_" + std::to_string(rep) + ".png").c_str()));
    *downloadName = *("modified_image_" + std::to_string(rep) + ".png").c_str();

    std::cout << downloadName << " 完成。" << std::endl;

    // 将修改后的图像写入磁盘
    stbi_write_png(downloadName, width, height, channels, modified_image, width * channels);

    free(downloadName);
}

我不太确定我做错了什么;这是我第一次使用malloc()和.c_str(),所以也许我忽略了一些明显的东西。

"stbi_write_png()" 函数接受一个 "const char *" 作为文件名(这是第一个参数),这就是为什么我不能只使用 std::string 的原因。

感谢您的帮助,谢谢!

英文:

I'm using the stb library to edit an image and then I'm trying to save the image to disk. The image making process works perfectly fine, however the name generating process fails and makes files called "mnn" and "mfn". These require me to add the ".png" extension at the end, which then allows me to actually view the image (and they look great btw).

This is my code below:

for(unsigned int rep = 0; rep &lt; 3; rep++) {
	//Execute image transformation
	create_square(50, 75, 100, 300, rand()%255, rand()%255, rand()%255);

	char* downloadName = (char *)malloc(sizeof( (&quot;modified_image_&quot; + std::to_string(rep) + &quot;.png&quot;).c_str() ));
	*downloadName = *(&quot;modified_image_&quot; + std::to_string(rep) + &quot;.png&quot;).c_str();

	std::cout &lt;&lt; downloadName &lt;&lt; &quot; completed.&quot; &lt;&lt; std::endl;

	//Write modified image to disk
	stbi_write_png(downloadName, width, height, channels, modified_image, width*channels);

	free(downloadName);
}

I'm not super sure what I'm doing wrong; this is one of my first times using malloc() and .c_str(), so perhaps I'm missing something obvious.

The "stbi_write_png()" function takes in a "const char *" as the filename(which is the first attribute), which is why I can't just use a std::string.

Any help is appreciated, thank you!

答案1

得分: 1

好的,我正在撰写此评论来回答我的问题并标记为已回答。

原来,我很傻!malloc() 收到了来自 .c_str() 的指针的大小,它返回一个指针。虽然我知道这一点,但我没有考虑到。现在我的代码如下:

for(unsigned int rep = 0; rep < 3; rep++) {
		//执行图像转换
		create_square(50, 75, 100, 300, rand()%255, rand()%255, rand()%255);

		//将修改后的图像写入磁盘
		stbi_write_png(("modified_image_" + std::to_string(rep) + ".png").c_str(), width, height, channels, modified_image, width*channels);
	}

这样,代码既能正常运行,而且更简洁。谢谢大家。

英文:

Okay I'm writing this comment to answer my own question and mark as answered.

Turns out, I was being dumb! The malloc() was recieving the size of the pointer from .c_str(), which returns a pointer. While I knew this, I was not considering. Now my code reads:

for(unsigned int rep = 0; rep &lt; 3; rep++) {
		//Execute image transformation
		create_square(50, 75, 100, 300, rand()%255, rand()%255, rand()%255);

		//Write modified image to disk
		stbi_write_png((&quot;modified_image_&quot; + std::to_string(rep) + &quot;.png&quot;).c_str(), width, height, channels, modified_image, width*channels);
	}

Which both, well, works and is much cleaner. Thanks folks.

huangapple
  • 本文由 发表于 2023年6月5日 07:10:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76402763.html
匿名

发表评论

匿名网友

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

确定