我正在用 C 语言编写一个程序来从 CS50 的原始文件中恢复图像,但遇到了一个奇怪的问题。我有一个变量 int cnt,用于调试目的,并且我让程序正常工作,因此我删除了剩余的调试代码。但是当我删除 cnt 声明时,我开始输出损坏的文件。 在删除下面的第 25 行之前,我输出了可以打开和查看的 .jpg 文件,然后删除了该行,重新编译,删除了上次运行的照片,并在相同的 .raw 数据和我得到的新文件上重新运行程序未被认可。于是我把声明放回去,重新编译,删除旧照片,然后再次运行程序,得到了好的文件。有谁知道为什么删除未使用的声明会扰乱我的结果?有问题的声明位于第 25 行。
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
int main(int argc, char *argv[])
{
if (argc != 2)
{
printf("Usage: ./recover image\n");
return 1;
}
int filesFound = 0;
FILE *inFile = fopen(argv[1], "r");
FILE *outFile = NULL;
if (inFile == NULL)
{
printf("Image file could not be opened\n");
return 1;
}
uint8_t buffer[512];
int cnt = 0;
while (!feof(inFile))
{
fread(buffer, 512, 1, inFile);
// check for start of jpg file
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
{
// start of jpg was found
if (outFile != NULL)
{
// close the current file and then open a new file to write to
fclose(outFile);
outFile = NULL;
}
// open a file to write to
char fName[4];
sprintf(fName, "%03i.jpg", filesFound);
outFile = fopen(fName, "w");
filesFound++;
}
if (outFile != NULL){
// we have found data to write and opened a file
fwrite(buffer, 512, 1, outFile);
}
}
//Be sure to close my files
fclose(inFile);
if (outFile != NULL)
{
fclose(outFile);
}
return 0;
}
最佳答案
字符fName[4]
没有足够的空间容纳 "%03i.jpg"
生成的名称,所以你超出了缓冲区。将其放大并使用snprintf
,不是sprintf
,并测试返回值以检测错误:
int result = snprintf(fName, sizeof fName, "%03i.jpg", filesFound);
if (sizeof fName <= result)
{
fprintf(stderr, "Internal error, buffer is too small for file name.\n");
exit(EXIT_FAILURE);
}
您可以使用 snprintf
的返回值,而不是打印错误。 ,它指示所需的长度,为更大的缓冲区分配内存,然后重做 snprintf
使用该缓冲区。
(请注意,如果发生错误,snprintf
可能会返回负结果。通常情况下,转换为size_t
进行比较时,它会变成一个很大的数字,因此会触发此错误消息。但是,在稳健的情况下,程序中,您可能需要为 result < 0
插入一个单独的测试。)
关于C - 为什么删除未使用的变量声明时我的代码会中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60080952/