views:

47

answers:

2
#define fileSize 100000 
int main(int argc, char *argv[]){       
        char *name=argv[1];
        char ret[fileSize];
        FILE *fl = fopen(name, "rb");
        fseek(fl, 0, SEEK_END);
        long len = fileSize;
        fseek(fl, 0, SEEK_SET);
        //fread(ret, 1, len, fl);
        int i;
        *(ret+fileSize) = '\0';
        for (i=0; i<fileSize; i++){
            *(ret+i)=fgetc(fl);
            printf("byte : %s \n", ret);
        }
        fclose(fl);
}

In the above code, when I feed the name of a jpeg file, it reads anything after the 4th character as ' '...any ideas? Thanks!

+4  A: 

This is because the %s is trying to print out a string. It detects the end of the string by finding the null character (byte value of 0). So, it's probably not printing out a space at all, but rather printing out nothing, or an empty string, because it encounters a byte with a value of 0.

Kibbee
+1  A: 

I would tend to agree that you are reading a zero byte,and suggest that you use %d as your format character, although I personally prefer hex and would use

  printf("byte : 0x%02X \n", ret);

But, I have a question for you. In a program so small, why ask us? I am not being sarcastic, I honestly wonder why you don't debug it yourself. Just build it in Eclipse and step through, a line at a time, and Eclipse will show you the values of all local variables and it should leap out at you what is wrong. Again - no offence intended.

Btw, you can find the actual JPEG file format here.

Mawg