0

I'm re-wrote this code and did not get any output or error. Tried valgrind and nothing was leaked.

I suspect it's either:

1) my fseek is not moving my cursor in inpt 'card.raw'

2) and/or.. because I'm using the &store[i] wrongly.. because I'm still abit confused about pointers.

How do i use fseek to move what I'm about to read into the store[i] array? do i need the & sign?..

#include <stdio.h>
#define MEMORYBLOCK 512
#include <cs50.h>
#include "jpg.h"

bool jpgCheck(BYTE data[], BYTE jpgSig[]);

int main(void)
{
//declare variables
BYTE store[MEMORYBLOCK];       //int == 4 bytes, we want, 1 byte 
char title[sizeof "###.jpg"];
int fileNameCounter = 0;
BYTE jpgSig[3] = { 0xff, 0xd8, 0xff };

     //-ZYMAYLA'S concept explaination
    //1) -Open memory card file [fopen("card.raw", "r"]
    FILE* inptr = fopen("card.raw", "r");
        if(inptr == NULL)
        {
            printf("could not open file\n");
            fclose(inptr);
            return 1;
        }
    //do
    do
    {
        //read & store 
        for(int i = 0; i < MEMORYBLOCK; i++)
        {
            //read & store 512bytes in memory[i]
            fread(&(store[i]), 1, 1, inptr);        //do we need *inptr instead?

        }
        //if find jpg, decide what to do. //if jpgCheck true, write file.  [read 512 bytes at a time] [compare first 4bytes, jpgCheck] [close jpg if any (on second pass?)]
        if(jpgCheck(store, jpgSig)) //is true
        {

            //2) -Open a new jpg [name it accordingly]            //fopen(title, "w");
            sprintf(title, "%03d.jpg", fileNameCounter);
            //open the newly created file
            FILE* outptr = fopen(title, "w");
            if(outptr == NULL)
            {
                printf("could not create file\n");
                fclose(outptr);
                return 2;
            }
            //keep track of file name counter
            fileNameCounter++;


            //3) -write 512bytes for "non-star files" until new jpeg is found [i.e line 2)] //if jpgCheck is not true, write. //else(true), close current file and go to line 2.        
            for(int i = 0; i < MEMORYBLOCK; i++)
            {
                fwrite(&(store[i]), 1, 1, outptr);
            }

            fseek(inptr, 512, SEEK_CUR);

            do
            {
                //read
                for(int i = 0; i < MEMORYBLOCK; i++)
                {
                    fread(&(store[i]), 1, 1, inptr);

                }
                //if not jpg, write. 
                if(jpgCheck(store, jpgSig) != 0)
                {
                    //write
                    for(int i = 0; i < MEMORYBLOCK; i++)
                    {
                        fwrite(&(store[i]), 1, 1, outptr);
                    }
                    //move cur
                    fseek(inptr, 512, SEEK_CUR);
                }

            }while(jpgCheck(store, jpgSig) == false); //is not start of jpg file

                      //if not first file, close previosu opened file
            if(fileNameCounter > 1)
            {
                fclose(outptr);
            } 
        }
        //else jpgCheck not true,!=, then, fseek, and keep looking
        else
        {
            fseek(inptr, 512, SEEK_CUR);
        }

    }while(feof (inptr)); //returns non-zero value at end of file     //while(fread(&bug, sizeof(DOG), 2, ptr) == 2)?
    //4) -Detect end of file (e.g. once it returns 1 instead of 2 DOGs          fun fact: when using functions in a condition, it will execute the function while checking for the condition...
                                                                                             //if(fread(&bug, sizeof(DOG), 2, ptr) == 1) <--that's how to check for end the file.
    fclose(inptr);
}

//function to JpgCheck
bool jpgCheck(BYTE store[], BYTE jpgSig[])
{
    for (int i = 0; i < 2; i++)
    {
        if ((store[i]) != (jpgSig[i]))
        {
            return false;
        }
    }
    if (store[3] < 0xe0 || store[3] > 0xef)
    {
        return false;
    }
    return true;
}

My jpg.h file

/**
 * Jpg.h
 * 
 * CS50 pset4 
 * 
 * '.h' file for refrence by recover.c
 * 
 * This file includes jpeg type data files
 * 
 **/

#include <stdint.h>
typedef uint8_t BYTE;
2
for(int i = 0; i < MEMORYBLOCK; i++)
    {
        //read & store 512bytes in memory[i]
        fread(&(store[i]), 1, 1, inptr);        //do we need *inptr instead?

    }

If you want to read 512 bytes into an array, then you can simply call fread once, like this:

fread(store, sizeof(BYTE), MEMORYBLOCK, inptr);

Also, you shouldn't need to use fseek in this program at all. When you fread, the file 'cursor' is moved forward ready to read in the next part of the file. If you fseek 512 bytes, you will be skipping those bytes.

edit to answer questions from comments:

When you do this BYTE store[MEMORYBLOCK]; you are declaring an array to hold 512 bytes. store is the name of that array. When you call fread and give it store as the destination, you are telling fread to read the bytes into that 512 byte block. There's no need to use the bracket notation. It will still work with fread(&block[i], ...) for example, or alternatively fread(block+i, ...), but if you are reading in one block of 512 and putting those into memory, it is much quicker to read in 512 in one go. Each fread operation takes time, so reading in the entire raw file one byte of a time might take 20,000,000 freads.

| improve this answer | |
  • Thanks for that, it's definitely cleaner and made my code easier to vet. It really worked out for me. Though, two questions if you don't mind me asking.. (1) In your suggestion, 'fread(store, sizeof(BYTE), MEMORYBLOCK, inptr);' you mentioned that "store" is an array. It's an array without [i]? isn't it just a variable if it doesn't have []square brackets? is your example one array with 512 space or... 512 arrays with 1 byte each? 2) just curious, could my (mistake) method have technically work too? – olafironfoot Dec 13 '16 at 18:40
  • 1
    I edited my answer to address those questions. – curiouskiwi Dec 13 '16 at 19:15

You must log in to answer this question.

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