c - C - 为什么我的代码在删除未使用的变量声明时会中断
问题描述
我正在用 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;
}
解决方案
charfName[4]
没有足够的空间容纳由 生成的名称"%03i.jpg"
,因此您超出了缓冲区。将其变大并使用snprintf
, notsprintf
并测试返回值以检测错误:
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# - C#:下载速度之间的差异
- php - 如何创建一个mysql网站,每个用户都有自己的私人信息,他们可以插入更改编辑删除
- sql - 如何在 PostgreSQL 中获取按 x 轴按月和 y 轴按小时分组的计数表?
- wordpress - WordPress下的htaccess RewriteRule
- c# - 是否可以在 ApplicationUser 类中添加角色作为外键?
- html - 如何在一个单词中有一个跨度而不打破它?
- java - 使用 PostgreSQL 在 Spring Boot 中的 JpaRepository 查询中返回列名
- c - visual studio 2017 srand() 崩溃(在线编译器工作)
- java - AES256 加密
- java - conn.rollback() 上的错误未处理 SQLException...如何正确使用 commit()?