Jump to content

Guardar num file o conteúdo de unsigned char[]


djthyrax
 Share

Recommended Posts

Boas, estou a usar uma lib que tem esta struct:

typedef struct 
{
    short version;
    unsigned char cbeff[LIB_MAX_CBEFF_LEN];
    unsigned char facialrechdr[LIB_MAX_FACRECH_LEN];
    unsigned char facialinfo[LIB_MAX_FACINFO_LEN];
    unsigned char imageinfo[LIB_MAX_IMAGEINFO_LEN];
    unsigned char picture[LIB_MAX_PICTUREH_LEN];
    unsigned long piclength;
} LIB_PIC;

E quero guardar o elemento picture num ficheiro. Estou a usar este código:

FILE *fp = fopen("imagem.jp2", "w");
fwrite(pic.picture, sizeof(unsigned char), pic.piclength, fp);
fclose(fp);

Ora, eu sei que o conteudo do picture é um .jp2 (JPEG 2000) válido, pois tenho aqui um exemplo em Java do uso desta lib em que eles se limitam a chamar a função que preenche o pic e depois tudo o que fazem é criar um FileOutputStream e fazer write para o ficheiro do pic.picture. No entanto, quando abro o imagem.jp2 que eu gerei com o código que deixei aqui, o irfanview diz que deu erro a ler o header. Abri o ficheiro com um editor hex, e os magic numbers () em vez de serem:

00 00 00 0C 6A 50 20 20 0D 0A 87 0A 00 00 00 14 66 74 79 70 6A 70 32

são

00 00 00 0C 6A 50 20 20 0D 0D 0A 87 0D 0A 00 00 00 14 66 74 79 70 6A 70 32

Ou seja, o há um \r\n que passou a \r\r\n durante a escrita, e um \n que passou a \r\n.

O que é que estou a fazer mal?

Não peças ajuda por PM! A tua dúvida vai ter menos atenção do que se for postada na secção correcta do fórum!

Link to comment
Share on other sites

Já agora, isto acontece por estares no Windows - nos SOs Unix-like não existe distinção entre ficheiros abertos com "w" e com "wb".

❝The idea that I can be presented with a problem, set out to logically solve it with the tools at hand, and wind up with a program that could not be legally used because someone else followed the same logical steps some years ago and filed for a patent on it is horrifying.❞- John Carmack on software patents

A list  of command line apps

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.