Why does this code read all ' ' for the anything after the 4th character?

#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!

Answers


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.


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.


Need Your Help

Write the output from your Instrument class methods to a text file that a user entered from the command line arguments

java class methods

I'm taking Java Introductory Programming class and currently i'm working on a final project. The assignment description can be found here link

How to wrap node js app into a rails gem

ruby-on-rails heroku gem rubygems

I have a nodeJS app that does some calculations. I'm curious if there's a way(and makes sense) creating a rails gem for my app that'd run that nodeJS app and communicate with rails (I'm using heroku)