I need your help again. Something wrong with my fclose in both cases (as valgrind said). The program compiles, but it has segmentation fault and doesn't create jpgs. Here is the code (sorry for posting it all, but i'm afraid that if i'll post a part of it it won't show the problem) :

#define BLOCK 512
typedef uint8_t BYTE;

int main(int argc, char* argv[]) { // open card file FILE* inptr = fopen("card.raw", "r"); if (inptr == NULL) { fclose(inptr); printf("Can't open %s.\n", "card.raw"); return 1; }

// create buffer BYTE buffer[BLOCK]; int counter = 0; FILE* outptr = NULL; // repeat until the end of file if (fread(&buffer, sizeof(buffer), 1, inptr) == 1) { // read BLOCK (512 bytes) into a buffer for (int b = 0; b < BLOCK; b++) { // start of a new jpg? if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] == 0xe0 || buffer[3] == 0xe1)) { // close the previous jpg if it was if (outptr != NULL) { fclose (outptr); } // create a jpg and give it a number/name char title[8]; sprintf(title, "%03d.jpg", counter); counter ++; outptr = fopen(title, "w"); // a? // check if file can be written if(outptr == NULL) { printf("Can't write the jpg.\n"); return 1; } // write into the file fwrite(&buffer, sizeof(buffer), 1, outptr); } else if (outptr != NULL) { // continue writting fwrite(&buffer, sizeof(buffer), 1, outptr); } } } // close card.raw fclose(inptr); // close the last jpg fclose (outptr); return 0; }

No, there's nothing wrong with the fclose() statements. You're getting a seg fault because you're trying to close a file that was never opened - outptr.

The real problems occur earlier in your program. If you were to insert some diagnostic printf() statements strategically, or walk through the program in gdb, then you would have seen that you are reading and processing exactly one 512 byte block of data and then moving on to the fclose() statements.

You have two problems in your code. First is this:

// repeat until the end of file
if (fread(&buffer, sizeof(buffer), 1, inptr) == 1)

It reads into the buffer once and only once. WHILE your thoughts loop around and you think about IF this is the best way to do this (pun intended ;-) ) , I'll move on.

Next, here's one I just don't understand. What purpose does this serve?

for (int b = 0; b < BLOCK; b++)

All I see this doing is reprocessing each 512 byte block 512 times! That also means that you're going to open 512 * 16 = 8192 files! Seems like a problem to me.

This should get you a long way down the road.

As a side note, I found it interesting that your code as written, but after reversing the order of the two fclose functions, the fclose(inptr) was seg faulting. It shouldn't have, but that's an interesting question to probe later, if anyone's curious.

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

  • Thank you! this is the second time you are saving me! :) I feel myself so stupid! Nov 18 '15 at 13:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .