"James Harris" <james.harris.1 @googlemail.com wrote in message
news:cc1bcdda-4e39-488a-b921-I'm not a C expert and I had to run your code
to find out what was
wrong. The fread call is specified thus
>
>
If you want to use it you can see that two things are important. First
it reads what you've told it to read into a location in memory. That
is the meaning of, "storing them at the location given by ptr" in the
description. You really don't want to be using fread if you are new to
C (keep it simple) but if you use it you need to supply it the /
address/ of where the value if to be stored. C gets the address by
using "&" so if the variable is cval, say, you tell fread the address
of cval by &cval.
>
Second, since you've told fread to read one byte your target should be
one byte long. In C this is the char datatype. So, if you change your
fread code to
>
#include <stdio.h>
>
int main() {
char cval; //Note this uses a char
FILE *fp;
fp = fopen("b", "rb");
fread(&cval, 1, 1, fp); //Note, address of cval
fclose(fp);
printf("%x\n", cval);
return 0;
}
>
>
If you want to use it you can see that two things are important. First
it reads what you've told it to read into a location in memory. That
is the meaning of, "storing them at the location given by ptr" in the
description. You really don't want to be using fread if you are new to
C (keep it simple) but if you use it you need to supply it the /
address/ of where the value if to be stored. C gets the address by
using "&" so if the variable is cval, say, you tell fread the address
of cval by &cval.
>
Second, since you've told fread to read one byte your target should be
one byte long. In C this is the char datatype. So, if you change your
fread code to
>
#include <stdio.h>
>
int main() {
char cval; //Note this uses a char
FILE *fp;
fp = fopen("b", "rb");
fread(&cval, 1, 1, fp); //Note, address of cval
fclose(fp);
printf("%x\n", cval);
return 0;
}
understanding of what it means as you say the address of. I also tried fgetc
to get one char but fgetc returns an int so I don't know if that would work
or not.
Don't give up on C.
It's worth learning but it is pedantic so you need
to make sure you use it and its library calls correctly. If you have
further C questions check the FAQ on comp.lang.c and if you don't find
an answer ask on that group. Ignore them if they are grupmy but there
is the best group for answers to C questions.
>
further C questions check the FAQ on comp.lang.c and if you don't find
an answer ask on that group. Ignore them if they are grupmy but there
is the best group for answers to C questions.
>
Bill
Comment