sprintf() 用法和 gdb 报告程序提前跳出循环?
sprintf() usage, and gdb reporting program breaking out of loop early?
我正在为 class 构建一个程序,它以 512 字节的块的形式从原始文件中提取所有字节,在每个块的开始字节中搜索 JPG 签名,并在找到 JPG 签名后写入出一张JPG图片。我已经成功地从原始文件中提取了一张 JPG,但是课程显示还有更多。在 GDB 中调试后,我注意到变量 "jpgname" 似乎从来没有一个值,而且,通过程序,我经常进入 "fclose(inptr)" 行,就好像它在主 while() 循环。我是否错误地使用了 GDB?我是否错误地使用了 sprintf() ?如果您对此问题有任何反馈,我将不胜感激。
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
typedef uint8_t BYTE;
int main(void)
{
BYTE jpg1[4] = {0xff, 0xd8, 0xff, 0xe0};
BYTE jpg2[4] = {0xff, 0xd8, 0xff, 0xe1};
int open = 0;
FILE* outp;
char* jpgname = malloc(sizeof(char) * 8);
int counter;
//TODO - MAKE sure this isn't a bunch of garbage data
//open card file
FILE* inptr = fopen("card.raw", "r");
if (inptr == NULL)
{
printf("Could not open file\n");
return 2;
}
BYTE buffer[512];
fread(buffer, 512, 1, inptr);
//repeat until end of card
while(fread(buffer, 512, 1, inptr) > 0)
{
counter = 0;
//start of new jpg?
if((buffer[0] == jpg1[0] && buffer[1] == jpg1[1] && buffer[2] == jpg1[2] && buffer[3] == jpg1[3]) || (buffer[0] == jpg2[0] && buffer[1] == jpg2[1] && buffer[2] == jpg2[2] && buffer[3] == jpg2[3]))
{
sprintf(jpgname, "%d.jpg", counter);
if (open == 1)
{
//close
fclose(outp);
//name new file
outp = fopen(jpgname, "w");
//write new
fwrite(buffer, sizeof(buffer), 1, outp);
counter++;
}
else if (open == 0)
{
//write new file
outp = fopen(jpgname, "w");
fwrite(buffer, sizeof(buffer), 1, outp);
open = 1;
counter++;
}
}
else
{
if(open == 1)
{
fwrite(buffer, sizeof(buffer), 1, outp);
}
}
}
fclose(outp);
fclose(inptr);
//close any remaining files
}
BYTE buffer[512];
fread(buffer, 512, 1, inptr);
//repeat until end of card
while(fread(buffer, 512, 1, inptr) > 0)
您应该检查为什么决定删除读入的开头 512 个字节。
FILE* inptr = fopen("card.raw", "r");
您应该以 binary 模式打开输入文件和输出文件,例如:
FILE* inptr = fopen("card.raw", "rb");
outp = fopen(jpgname, "wb");
否则读入或写出的数据可能会乱码
虽然我感谢大家的意见并使我的代码变得更好,但导致程序根本无法运行的错误是由于我的计数器在循环开始时被重置为 0。
while(fread(buffer, 512, 1, inptr) > 0)
{
counter = 0;
...
}
应该是
counter = 0;
while(fread(buffer, 512, 1, inptr) > 0)
{
...
}
我正在为 class 构建一个程序,它以 512 字节的块的形式从原始文件中提取所有字节,在每个块的开始字节中搜索 JPG 签名,并在找到 JPG 签名后写入出一张JPG图片。我已经成功地从原始文件中提取了一张 JPG,但是课程显示还有更多。在 GDB 中调试后,我注意到变量 "jpgname" 似乎从来没有一个值,而且,通过程序,我经常进入 "fclose(inptr)" 行,就好像它在主 while() 循环。我是否错误地使用了 GDB?我是否错误地使用了 sprintf() ?如果您对此问题有任何反馈,我将不胜感激。
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
typedef uint8_t BYTE;
int main(void)
{
BYTE jpg1[4] = {0xff, 0xd8, 0xff, 0xe0};
BYTE jpg2[4] = {0xff, 0xd8, 0xff, 0xe1};
int open = 0;
FILE* outp;
char* jpgname = malloc(sizeof(char) * 8);
int counter;
//TODO - MAKE sure this isn't a bunch of garbage data
//open card file
FILE* inptr = fopen("card.raw", "r");
if (inptr == NULL)
{
printf("Could not open file\n");
return 2;
}
BYTE buffer[512];
fread(buffer, 512, 1, inptr);
//repeat until end of card
while(fread(buffer, 512, 1, inptr) > 0)
{
counter = 0;
//start of new jpg?
if((buffer[0] == jpg1[0] && buffer[1] == jpg1[1] && buffer[2] == jpg1[2] && buffer[3] == jpg1[3]) || (buffer[0] == jpg2[0] && buffer[1] == jpg2[1] && buffer[2] == jpg2[2] && buffer[3] == jpg2[3]))
{
sprintf(jpgname, "%d.jpg", counter);
if (open == 1)
{
//close
fclose(outp);
//name new file
outp = fopen(jpgname, "w");
//write new
fwrite(buffer, sizeof(buffer), 1, outp);
counter++;
}
else if (open == 0)
{
//write new file
outp = fopen(jpgname, "w");
fwrite(buffer, sizeof(buffer), 1, outp);
open = 1;
counter++;
}
}
else
{
if(open == 1)
{
fwrite(buffer, sizeof(buffer), 1, outp);
}
}
}
fclose(outp);
fclose(inptr);
//close any remaining files
}
BYTE buffer[512];
fread(buffer, 512, 1, inptr);
//repeat until end of card
while(fread(buffer, 512, 1, inptr) > 0)
您应该检查为什么决定删除读入的开头 512 个字节。
FILE* inptr = fopen("card.raw", "r");
您应该以 binary 模式打开输入文件和输出文件,例如:
FILE* inptr = fopen("card.raw", "rb");
outp = fopen(jpgname, "wb");
否则读入或写出的数据可能会乱码
虽然我感谢大家的意见并使我的代码变得更好,但导致程序根本无法运行的错误是由于我的计数器在循环开始时被重置为 0。
while(fread(buffer, 512, 1, inptr) > 0)
{
counter = 0;
...
}
应该是
counter = 0;
while(fread(buffer, 512, 1, inptr) > 0)
{
...
}