c-在涉及fread的情况下退出程序



我正在为我正在学习的课程编写一个照片提取程序。

对于这个程序,argv[1]是我要从中提取图像的文件的名称。jpg_name与我正在提取的每个图像的名称相关;我正在尝试为每个jpg命名。数值从1开始。由于有50张照片要提取,我想在提取完所有50张图像后停止程序。不幸的是,我的程序不确定何时终止,因此我认为最后一张照片被多次覆盖,我不确定如何解决此问题。然而,照片1-49结果很好,这只是照片50我有问题。

到目前为止,我尝试过的事情包括实现一个涉及freadif条件,如果程序无法将512字节的代码读取到数组中,它将终止。不幸的是,这似乎也不起作用。任何建议都将不胜感激:

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int main(int argc, char *argv[])
{
if (argc != 2)
{
printf("Invalid entry.n");
return 0;
}
int counter = 1;
FILE* images;
char jpg_name[8];
// Check if bytes are jpg. signatures.
for (int n = 0; counter < 51; n = n + 512)
{
// Open file for reading.
FILE *file = fopen(argv[1], "r");
if (!file)
{
return 1;
}
unsigned char array[512];
fseek(file, n, SEEK_SET);
fread(array, 1, 512, file); // if EOF, won't have 512 to write into!!!
if (fread(array, 1, 512, file) != 512)
{
return 2;
}
if (array[0] == 0xff && array[1] == 0xd8 && array[2] == 0xff && (array[3] & 0xf0) == 0xe0)
{
// Convert integer to string and store into jpg character array. Increment image number.
sprintf(jpg_name, "%03i.jpg", counter);
counter++;
// Open images file to write into, allocate memory to jpg file to write into, write 512 bytes from array into image file.
images = fopen(jpg_name, "a");
fwrite(array, 1, 512, images);
fclose(images);
}
else // If 1st 4 bytes aren't jpg signature.
{
if (counter > 1)
{
images = fopen(jpg_name, "a");
fwrite(array, 1, 512, images);
fclose(images);
}
}
fclose(file);
}
}

您似乎对函数和返回调用有一个基本的误解。我怀疑您认为:fread指令读取数据,而if指令检查某些内容是否为真。所以你读取数据,然后检查读取了多少。但是,您忽略了if执行()内部的指令以查看结果是否为真的事实。因此if(fread(array, 1, 512, file) != 512)读取数据检查结果。

如果要读取数据并检查结果,请使用if(fread(array, 1, 512, file) != 512)。如果要读取数据而不检查结果,请使用fread(array, 1, 512, file);。不要两者都用。

您也可以选择使用一个变量,例如:

int number_of_bytes_read = fread(array, 1, 512, file);
if(number_of_bytes_read != 512) // this doesn't read data, it just gets the number from the variable
{
...

如果我理解正确,您正试图从一个jpeg文件中提取图像,并将其拆分为多个文件。以下是关于的几个问题

  1. fseek,为什么需要它,除非你试图转到文件的末尾获取长度并恢复到当前偏移量。否则,我建议你把它去掉
  2. 不需要两次运费。此外,如果fread提供了你想要的以外的任何东西,feof((应该会告诉你是否已经到达eof

我稍微编辑了一下你的代码,看看这是否是你想要的


#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>

int main(int argc, char *argv[])
{
if (argc != 2)
{
printf("Invalid entry.n");
return 0;
}

int counter = 1;
FILE* images;
char jpg_name[8];
int num = 0;
int total_num = 0;

// Check if bytes are jpg. signatures.
for (int counter = 0; counter < 51; counter ++)
{
// Open file for reading.
FILE *file = fopen(argv[1], "r");
if (!file)
{
printf("Exiting at 1n");
return 1;
}

unsigned char array[512];
// don't understand why you need this ? unless, you are trying to go to end of file and get back to the current
// offset
fseek(file, 0, SEEK_SET);
total_num = 0;
while((num = fread(array, 1, 512, file)) == 512)
{
if (array[0] == 0xff && array[1] == 0xd8 && array[2] == 0xff && (array[3] & 0xf0) == 0xe0)
{
// Convert integer to string and store into jpg character array. Increment image number.
sprintf(jpg_name, "%03i.jpg", counter);
counter++;

// Open images file to write into, allocate memory to jpg file to write into, write 512 bytes from array into image file.
images = fopen(jpg_name, "a");
fwrite(array, 1, 512, images);
fclose(images);
}
else // If 1st 4 bytes aren't jpg signature.
{
if (counter > 1)
{
images = fopen(jpg_name, "a");
fwrite(array, 1, 512, images);
fclose(images);
}
}
}
fclose(file);
}
printf("Exiting heren");
}

最新更新