C Error: Cannot fopen a file using the same name after fclose it
C Error: Cannot fopen a file using the same name after fclose it
在这个任务中,我需要创建多个 JPEG 文件。每次,在我打开下一个文件之前,都会打开、写入和关闭一个文件。
然而,当我再次命名文件 outptr
时它显示错误,尽管我在打开新文件之前关闭了文件。
你能告诉我这里出了什么问题吗?
错误信息如下:
recover.c:51:23: error: declaration shadows a local variable [-Werror,-Wshadow]
FILE *outptr = fopen(outfile, "w");
^
recover.c:25:11: note: previous declaration is here
FILE *outptr = fopen("000.jpg", "w");
^
1 error generated.
这是我的原始代码:
int main(int argc, char *argv[])
{
// Number of bytes in a block
const int BLOCK_SIZE = 512;
// Open input file
FILE *infile = fopen(argv[1], "r");
if (infile == NULL)
{
fprintf(stderr, "Could not open %s.\n", argv[1]);
return 1;
}
//File name
char outfile[7];
// Create a buffer to copy data
int16_t buffer[512];
// Open a random file
FILE *outptr = fopen("000.jpg", "w");
int i = -1;
while (i < 50)
{
// While not end of memory card
while (!(buffer[0] == 0x00 && buffer[1] == 0x00 && buffer[2] == 0x00 && i == 50))
{
fread(buffer, sizeof(int16_t) * 512, 1, infile);
// Check if this block marks the beginning of a new picture
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff)
{
// Increase file number
i += 1;
// Close current output file
fclose(outptr);
// Output file name
sprintf(outfile, "%03d.jpg\n", i);
// Open new output file
FILE *outptr = fopen(outfile, "w");
if (outptr == NULL)
{
fclose(infile);
fprintf(stderr, "Could not create %s.\n", outfile);
return 1;
}
// Write buffer to new file
fwrite(buffer, sizeof(int16_t) * 512, 1, outptr);
}
else
{
// Write buffer to current file
fwrite(buffer, sizeof(int16_t) * 512, 1, outptr);
}
}
// Increase i to 50 and break the while loop
i += 1;
// Close current output file
fclose(outptr);
}
// Close files
fclose(infile);
}
您在代码块中重新定义了 outptr
。在同一个代码块中,您使用了外部作用域变量。这在 C 中是不合法的。修复是这样的:
将 while
内 if
块中的 FILE *outptr = fopen(outfile, "w");
替换为:
outptr = fopen(outfile, "w");
在这个任务中,我需要创建多个 JPEG 文件。每次,在我打开下一个文件之前,都会打开、写入和关闭一个文件。
然而,当我再次命名文件 outptr
时它显示错误,尽管我在打开新文件之前关闭了文件。
你能告诉我这里出了什么问题吗?
错误信息如下:
recover.c:51:23: error: declaration shadows a local variable [-Werror,-Wshadow]
FILE *outptr = fopen(outfile, "w");
^
recover.c:25:11: note: previous declaration is here
FILE *outptr = fopen("000.jpg", "w");
^
1 error generated.
这是我的原始代码:
int main(int argc, char *argv[])
{
// Number of bytes in a block
const int BLOCK_SIZE = 512;
// Open input file
FILE *infile = fopen(argv[1], "r");
if (infile == NULL)
{
fprintf(stderr, "Could not open %s.\n", argv[1]);
return 1;
}
//File name
char outfile[7];
// Create a buffer to copy data
int16_t buffer[512];
// Open a random file
FILE *outptr = fopen("000.jpg", "w");
int i = -1;
while (i < 50)
{
// While not end of memory card
while (!(buffer[0] == 0x00 && buffer[1] == 0x00 && buffer[2] == 0x00 && i == 50))
{
fread(buffer, sizeof(int16_t) * 512, 1, infile);
// Check if this block marks the beginning of a new picture
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff)
{
// Increase file number
i += 1;
// Close current output file
fclose(outptr);
// Output file name
sprintf(outfile, "%03d.jpg\n", i);
// Open new output file
FILE *outptr = fopen(outfile, "w");
if (outptr == NULL)
{
fclose(infile);
fprintf(stderr, "Could not create %s.\n", outfile);
return 1;
}
// Write buffer to new file
fwrite(buffer, sizeof(int16_t) * 512, 1, outptr);
}
else
{
// Write buffer to current file
fwrite(buffer, sizeof(int16_t) * 512, 1, outptr);
}
}
// Increase i to 50 and break the while loop
i += 1;
// Close current output file
fclose(outptr);
}
// Close files
fclose(infile);
}
您在代码块中重新定义了 outptr
。在同一个代码块中,您使用了外部作用域变量。这在 C 中是不合法的。修复是这样的:
将 while
内 if
块中的 FILE *outptr = fopen(outfile, "w");
替换为:
outptr = fopen(outfile, "w");