0

I know everyone's been using Byte[512] or something like that but I really wanna try using malloc.

I create a pointer "block" point it to an empty memory with 512 spaces. Then I read from raw and put all those 0's and 1's in my 512 byte and so I use block[??] to compare the bytes. Am i doing something wrong??? like every time fread was implimented block was set to 0. kinda confused

#include<stdio.h> #include<cs50.h> #include<stdlib.h> int main(int argc, char* argv[]) { int num = 0; FILE* inpt = fopen("card.raw", "r"); int* block = malloc(512); char* title = malloc(8); sprintf(title,"%d%d%d.jpg",(num-num%100-num%10)/100,(num%100-num%10)/10,num%10); FILE* out = fopen(title,"w"); while(fread(block,sizeof(block),1,inpt) == 1) { if(block[0]==0xff && block[1] == 0xd8 && block[2] == 0xff && block[3] >= 0xe0 && block[3] <= 0xef) { sprintf(title,"%d%d%d.jpg",(num-num%100-num%10)/100,(num%100-num%10)/10,num%10); fclose(out); out = fopen(title,"w"); num++; } if(num > 0) fwrite(block,sizeof(block),1,out); } fclose(out); fclose(inpt); free(block); free(title); 

}

1 Answer 1

1

The problem lies here:

int* block = malloc(512); 

Remember that an int is 4 bytes, not one, and it's messing with your code. If you change it to a one byte type, like uint8_t * block = malloc(512); it works, assuming your other code is correct.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.