user1131997
user1131997

Reputation:

Need to read data from png file and save it to the new one, how? C++

I try to read data from one PNG file, and want to write this data to the new file and save it. I do such stuff like that:

FILE *fp = fopen("C:\\dev\\1.png", "rb");
fseek(fp, 0, SEEK_END);
long size = ftell(fp);
rewind(fp);
char *buffer = (char*)malloc(sizeof(char)*size);
size_t result = fread(buffer, 1, size, fp);
FILE *tmpf = fopen("C:\\dev\\1_1.png", "wb");
fputs(buffer, tmpf);
fflush(tmpf);
fclose(tmpf);

I've got problem, that second file only has in its content, only that: ‰PNG SUB

In debugging , I have checked, long size = 652521, and size_t result has got the same size... Don't understand, why I can't write all data to the second file...

Upvotes: 2

Views: 1275

Answers (1)

Paul R
Paul R

Reputation: 212929

Don't use fputs - use fwrite - fputs is for strings and will terminate on the first zero byte.

Change:

fputs(buffer, tmpf);

to:

fwrite(buffer, 1, size, tmpf);

Upvotes: 4

Related Questions