0

I seem to have in issue writing data inside my loop, so that 50 JPGs are created with only 1 block of content each. I am checking for the beginning of a new JPG, and open a new output file inside that if statement. With help from gdb, I think I have figured out the problem is that my output file is no longer known once I come out of that if statement.

I have already tried to set up another do/while loop inside the if statement instead of writing outside, and that gave me 26 reasonably sized files that were still corrupt. What am I missing? How can I keep hold of my output file's address so I can continue to write to it?

while not EOF
{
   read data into buffer

   if (beginning of JPG)
    {
        close previous output file if exists
        create and open new output file    
        update counter
    }

    if (output file exists)
    {
        write one block of data
    }
}
close remaining files
free memory

And this is the relevant valgrind output:

==16338== HEAP SUMMARY:
==16338==     in use at exit: 17,600 bytes in 50 blocks
==16338==   total heap usage: 51 allocs, 1 frees, 17,952 bytes allocated
==16338== 
==16338== LEAK SUMMARY:
==16338==    definitely lost: 0 bytes in 0 blocks
==16338==    indirectly lost: 0 bytes in 0 blocks
==16338==      possibly lost: 0 bytes in 0 blocks  
==16338==    still reachable: 17,600 bytes in 50 blocks
==16338==         suppressed: 0 bytes in 0 blocks
0

I found the answer thanks to this thread.

I had initialized my output file as FILE* img = NULL; outside the while loop, and then reinitialized it inside the if statement like so: FILE* img = fopen(title, "a");

Removing the second FILE* did the trick, and the program now works!

You must log in to answer this question.

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