0

My problem here is that, I think..., everything works fine, but the actual block variable isn't changing. It is of BLOCK size, which is 512 bytes, the first for bytes of which are listed as block.head[0,1,2,3]. So my program uses block.head[] to see if the first four bytes of the chunk of the file that it is reading into match.

So what I think is happening is that it is reading BLOCK amount into the file, but it is not actually changing the block.head variable when it does that. So even though it is reading a certain part of the file, it is not changing the variable it needs to compare. I thought writing &block in the fread argument would change that but it isn't.

Any thoughts?

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

typedef uint8_t  BYTE;
typedef uint32_t DWORD;

typedef struct
{
    BYTE head[4];
    DWORD restofblock[127];
}
BLOCK;

int main (void)
{
    //open card.raw
    FILE* fp = fopen("file.file", "r");

    //default name for new files is new000.jpg
    char outfile[10] = "new000.jpg";

    // char* infile = argv[1];
    // FILE* inptr = fopen(infile, "r");

    FILE* output;
    output = NULL;

    //define a black
    BLOCK block;

    //While we haven't read past the end of the file
    while (block.head[1] != EOF)
    {
        //read a BLOCK of the .raw file
        fread(&block, sizeof(BLOCK), 1, fp);

        //dual "if" statements because I couldn't figure out how to combine them
        //checks to see if the first four BYTES of BLOCK block are a JPEG header
        if (block.head[0] == 255 && block.head[1] == 231 && block.head[2] == 255)
        {
            if (block.head[3] == 239 || block.head[3] == 240)
            {

                fclose(output);

                //designating c as a placeholder for the 3rd 0 in the filename
                //checks to see if c is above or equal to "9".
                //if it is, it resets "9" to "0" and increments the 2nd 0 in the filename by 1
                //if it isn't it adds 1 to the 3rd "0"
                char c = (outfile[5]);
                if (c >= 71) //71 is the ascii equivalent of 9
                {
                    outfile[5] -= 9;
                    outfile[4]++;
                }
                else
                    outfile[5]++;

                //Read forward 1 BLOCK to check for EOF, if EOF is false, read back to original position and print. 
                //Otherwise read back to initial position.
                fread(&block, sizeof(BLOCK), 1, fp);
                if(block.head[0] != EOF)
                {
                    fread(&block, -sizeof(BLOCK), 1, fp);
                    output = fopen(outfile, "w");
                }
                else
                    fread(&block, -sizeof(BLOCK), 1, fp);
            }
        }
        if (output == NULL);

        else
            fwrite(&block, sizeof(BLOCK), 1, output);

   }
    fclose(output);
    fclose(fp);
    return 0;
}
user2208569
  • 109
  • 1
  • 11
  • Perhaps a quick review of [`fread()`](http://en.cppreference.com/w/c/io/fread) might do you some good. The size parameter is NOT signed, and you cannot "rewind" using `fread()` like you appear to think. Consider [`fseek()`](http://en.cppreference.com/w/c/io/fseek) instead, because what you have now has no chance of working. – WhozCraig Apr 03 '13 at 05:17
  • Read `man fread`: "fread() does not distinguish between end-of-file and error, and callers must use feof(3) and ferror(3) to determine which occurred." Better use regular `read`. Also `if(block.head[0] != EOF)` will never works, as `EOF = -1`, but your `head` is unsigned. Also, as I mentioned before, `fread` don't return EOF! – Eddy_Em Apr 03 '13 at 05:57
  • I think, `mmap` and incrementing of pointer to `BLOCK` with size control of mmaped file will be much easy. – Eddy_Em Apr 03 '13 at 06:02

1 Answers1

0

Instead of

while (block.head[1] != EOF)

You should read like:

while (fread(&block, sizeof(BLOCK), 1, fp) > 0)

I couldn't find any place where you assign head[1] = EOF, Additionally initially block.head[1] has garbage value in that. because you don't initialized it and uses in while condition.

Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
  • Now that I put that in, there is a segmentation fault and it only writes one file that is 0bytes large. i.e. it creates it, then crashes – user2208569 Apr 03 '13 at 06:46