내 문제는 모든 것이 정상적으로 작동하지만 실제 블록 변수가 변경되지 않는다는 것입니다. BLOCK 크기로 512 바이트이며, 첫 번째 바이트는 block.head [0,1,2,3]으로 나열됩니다. 그래서 내 프로그램은 block.head []를 사용하여 읽고있는 파일의 청크의 처음 4 바이트가 일치하는지 확인합니다.fread를 사용하여 구조체 값을 변경하는 데 문제가 발생했습니다. C
그래서 내가 생각하는 것은 그것이 파일에 BLOCK 양을 읽는 것이지만, 실제로 block.head 변수를 변경하지 않는다는 것입니다. 따라서 파일의 특정 부분을 읽더라도 비교할 변수를 변경하지 않습니다. fread 인수에 & 블록을 쓰면 변경 될 것이라고 생각했지만 그렇지는 않습니다.
의견이 있으십니까? 대신
while (block.head[1] != EOF)
의
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
typedef uint8_t BYTE;
typedef uint32_t DWORD;
typedef struct
{
BYTE head[4];
DWORD restofblock[127];
}
BLOCK;
int main (void)
{
//open card.raw
FILE* fp = fopen("file.file", "r");
//default name for new files is new000.jpg
char outfile[10] = "new000.jpg";
// char* infile = argv[1];
// FILE* inptr = fopen(infile, "r");
FILE* output;
output = NULL;
//define a black
BLOCK block;
//While we haven't read past the end of the file
while (block.head[1] != EOF)
{
//read a BLOCK of the .raw file
fread(&block, sizeof(BLOCK), 1, fp);
//dual "if" statements because I couldn't figure out how to combine them
//checks to see if the first four BYTES of BLOCK block are a JPEG header
if (block.head[0] == 255 && block.head[1] == 231 && block.head[2] == 255)
{
if (block.head[3] == 239 || block.head[3] == 240)
{
fclose(output);
//designating c as a placeholder for the 3rd 0 in the filename
//checks to see if c is above or equal to "9".
//if it is, it resets "9" to "0" and increments the 2nd 0 in the filename by 1
//if it isn't it adds 1 to the 3rd "0"
char c = (outfile[5]);
if (c >= 71) //71 is the ascii equivalent of 9
{
outfile[5] -= 9;
outfile[4]++;
}
else
outfile[5]++;
//Read forward 1 BLOCK to check for EOF, if EOF is false, read back to original position and print.
//Otherwise read back to initial position.
fread(&block, sizeof(BLOCK), 1, fp);
if(block.head[0] != EOF)
{
fread(&block, -sizeof(BLOCK), 1, fp);
output = fopen(outfile, "w");
}
else
fread(&block, -sizeof(BLOCK), 1, fp);
}
}
if (output == NULL);
else
fwrite(&block, sizeof(BLOCK), 1, output);
}
fclose(output);
fclose(fp);
return 0;
}
아마도''fread()'] (http://en.cppreference.com/w/c/io/fread)에 대한 빠른 검토가 도움이 될 것입니다. size 매개 변수는 서명되지 않았고, 생각하는 것처럼'fread() '를 사용하여 "되감기"할 수 없습니다. 대신에''fseek()'] (http://en.cppreference.com/w/c/io/fseek)를 생각해보십시오. 왜냐하면 지금 가지고있는 것은 작동 할 기회가 없기 때문입니다. – WhozCraig
'man fread' 읽기 : "fread()는 파일 끝과 오류를 구별하지 않으며, 호출자는 feof (3)와 ferror (3)를 사용하여 발생한 것을 판별해야합니다." 일반 "읽기"를 사용하는 것이 더 좋습니다. 또한'if (block.head [0]! = EOF)'는'EOF = -1'처럼 작동하지 않지만'head'는 서명되지 않습니다. 또한 앞서 언급했듯이'fread'는 EOF를 반환하지 않습니다! –
mmaped 파일의 크기 조절로'mmap'과'BLOCK'에 대한 포인터를 증가시키는 것이 훨씬 쉽다고 생각합니다. –