Re: viewing mbr os descriptor

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bill Cunningham

    Re: viewing mbr os descriptor


    "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;
    }
    Very interesting. One thing I've not used in code is &. I have an
    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.
    I am not. No matter how long it takes. It's worth the patience.

    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.
    >
    Yes.

    Bill


  • Keith Thompson

    #2
    Re: viewing mbr os descriptor

    "Bill Cunningham" <nospam@nspam.c omwrites:
    [...]
    I also tried fgetc to get one char but fgetc returns an int so I
    don't know if that would work or not.
    [...]

    Yes, fgetc reads a single character. Yes, it returns a result of type
    int, not of type char.

    Look up fgetc in your C textbook (K&R2?), or in your system's
    documentation, or in the FAQ <http://www.c-faq.com>, or in the
    standard. Any of those sources should explain exactly *why* fgetc
    returns an int, and what you need to do with the result.

    Don't wonder. Don't guess. Look it up.

    --
    Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

    Comment

    Working...