How to use read() system call to read an 32bit integer

Discussion in 'C Programming' started by Yin Zhu, Sep 11, 2007.

  1. Yin Zhu

    Yin Zhu Guest

    Hello, All

    Maybe this question is not proper to post here:( but anyway would
    someone help me?

    I am doing my operating system homework. I know how to use a buffer
    string to do read and write.

    But how about 32-bit int?

    Here is my method to do:

    typedef unsigned char *byte_pointer;
    write(fd, (byte_pointer)&i, sizeof(int));
    read(fd, (byte_pointer)&j, sizeof(int));

    it works.

    I don't know whether my method is a common way. or there's some other
    ways to do it.


    thanks in advance.
     
    Yin Zhu, Sep 11, 2007
    #1
    1. Advertising

  2. In article <>,
    Yin Zhu <> wrote:
    >Hello, All
    >
    >Maybe this question is not proper to post here:( but anyway would
    >someone help me?
    >
    >I am doing my operating system homework. I know how to use a buffer
    >string to do read and write.
    >
    >But how about 32-bit int?
    >
    >Here is my method to do:
    >
    >typedef unsigned char *byte_pointer;
    >write(fd, (byte_pointer)&i, sizeof(int));
    >read(fd, (byte_pointer)&j, sizeof(int));


    Off topic. Not portable. Cant discuss it here. Blah, blah, blah.

    --
    Useful clc-related links:

    http://en.wikipedia.org/wiki/Aspergers
    http://en.wikipedia.org/wiki/Clique
    http://en.wikipedia.org/wiki/C_programming_language
     
    Kenny McCormack, Sep 11, 2007
    #2
    1. Advertising

  3. Yin Zhu

    jacob navia Guest

    Yin Zhu wrote:
    > Hello, All
    >
    > Maybe this question is not proper to post here:( but anyway would
    > someone help me?
    >
    > I am doing my operating system homework. I know how to use a buffer
    > string to do read and write.
    >
    > But how about 32-bit int?
    >
    > Here is my method to do:
    >
    > typedef unsigned char *byte_pointer;
    > write(fd, (byte_pointer)&i, sizeof(int));
    > read(fd, (byte_pointer)&j, sizeof(int));
    >
    > it works.
    >
    > I don't know whether my method is a common way. or there's some other
    > ways to do it.
    >
    >
    > thanks in advance.
    >


    In standard C you would use fread and fwrite instead of read and write.
    But please do not forget:

    If it works, it works. Do not fix something that's not broken
     
    jacob navia, Sep 11, 2007
    #3
  4. Yin Zhu

    Yin Zhu Guest

    On Sep 11, 9:11 pm, jacob navia <> wrote:
    > Yin Zhu wrote:
    > > Hello, All

    >
    > > Maybe this question is not proper to post here:( but anyway would
    > > someone help me?

    >
    > > I am doing my operating system homework. I know how to use a buffer
    > > string to do read and write.

    >
    > > But how about 32-bit int?

    >
    > > Here is my method to do:

    >
    > > typedef unsigned char *byte_pointer;
    > > write(fd, (byte_pointer)&i, sizeof(int));
    > > read(fd, (byte_pointer)&j, sizeof(int));

    >
    > > it works.

    >
    > > I don't know whether my method is a common way. or there's some other
    > > ways to do it.

    >
    > > thanks in advance.

    >
    > In standard C you would use fread and fwrite instead of read and write.
    > But please do not forget:
    >
    > If it works, it works. Do not fix something that's not broken



    but I need to compare fread with read.
    fread has its own buffering mechanism, I want to write a buffered read
    to compare with fread.
     
    Yin Zhu, Sep 11, 2007
    #4
  5. [comp.lang.c] jacob navia <> wrote:

    > Yin Zhu wrote:


    >> Maybe this question is not proper to post here:( but anyway would
    >> someone help me?


    comp.unix.programmer would have been a good place.

    > If it works, it works. Do not fix something that's not broken


    Just because something works doesn't mean it isn't broken.

    --
    C. Benson Manica | I appreciate all corrections, polite or otherwise.
    cbmanica(at)gmail.com |
    ----------------------| I do not currently read any posts posted through
    sdf.lonestar.org | Google groups, due to rampant unchecked spam.
     
    Christopher Benson-Manica, Sep 11, 2007
    #5
  6. Yin Zhu writes:
    > On Sep 11, 9:11 pm, jacob navia <> wrote:
    >> Yin Zhu wrote:
    >> In standard C you would use fread and fwrite instead of read and write.


    And open the file with "b" in the mode string, since it's presumably a
    binary file.

    --
    Hallvard
     
    Hallvard B Furuseth, Sep 11, 2007
    #6
  7. On Sep 11, 3:08 pm, (Kenny McCormack)
    wrote:

    > Off topic. Not portable. Cant discuss it here. Blah, blah, blah.



    why does he say that so often ? is he a bot ???
     
    Heinrich Pumpernickel, Sep 11, 2007
    #7
  8. Yin Zhu

    jacob navia Guest

    Christopher Benson-Manica wrote:
    > [comp.lang.c] jacob navia <> wrote:
    >
    >> Yin Zhu wrote:

    >
    >>> Maybe this question is not proper to post here:( but anyway would
    >>> someone help me?

    >
    > comp.unix.programmer would have been a good place.
    >
    >> If it works, it works. Do not fix something that's not broken

    >
    > Just because something works doesn't mean it isn't broken.
    >


    And what's wrong with that call to write or read?
    Outside the fact that is not ANSI (as I indicated to the OP) there is
    nothing wrong with it!
     
    jacob navia, Sep 11, 2007
    #8
  9. On 11 Sep, 15:17, Heinrich Pumpernickel <> wrote:
    > On Sep 11, 3:08 pm, (Kenny McCormack)
    > wrote:


    > > Off topic. Not portable. Cant discuss it here. Blah, blah, blah.

    >
    > why does he say that so often ? is he a bot ???


    just a troll, ignore him.

    --
    Nick Keighley
     
    Nick Keighley, Sep 11, 2007
    #9
  10. [comp.lang.c] jacob navia <> wrote:

    > And what's wrong with that call to write or read?
    > Outside the fact that is not ANSI (as I indicated to the OP) there is
    > nothing wrong with it!


    It's possible that the program was required to be written in standard
    C (we don't know), in which case it IS broken since it fails to meet
    the requirement.

    --
    C. Benson Manica | I appreciate all corrections, polite or otherwise.
    cbmanica(at)gmail.com |
    ----------------------| I do not currently read any posts posted through
    sdf.lonestar.org | Google groups, due to rampant unchecked spam.
     
    Christopher Benson-Manica, Sep 11, 2007
    #10
  11. Yin Zhu

    Willem Guest

    Yin wrote:
    ) Hello, All
    )
    ) Maybe this question is not proper to post here:( but anyway would
    ) someone help me?
    )
    ) I am doing my operating system homework. I know how to use a buffer
    ) string to do read and write.
    )
    ) But how about 32-bit int?
    )
    ) Here is my method to do:
    )
    ) typedef unsigned char *byte_pointer;
    ) write(fd, (byte_pointer)&i, sizeof(int));
    ) read(fd, (byte_pointer)&j, sizeof(int));

    What's wrong with:

    write(fd, &i, sizeof(i));
    read(fd, &i, sizeof(i));

    ?


    SaSW, Willem
    --
    Disclaimer: I am in no way responsible for any of the statements
    made in the above text. For all I know I might be
    drugged or something..
    No I'm not paranoid. You all think I'm paranoid, don't you !
    #EOT
     
    Willem, Sep 11, 2007
    #11
  12. Yin Zhu

    Tor Rustad Guest

    Yin Zhu wrote:

    > I am doing my operating system homework. I know how to use a buffer
    > string to do read and write.


    Huh.. is this really OS homework?!


    > But how about 32-bit int?
    >
    > Here is my method to do:
    >
    > typedef unsigned char *byte_pointer;
    > write(fd, (byte_pointer)&i, sizeof(int));
    > read(fd, (byte_pointer)&j, sizeof(int));
    >
    > it works.


    It shouldn't, unless your code runs in *user* space. write() and read()
    are POSIX system calls, but in *kernel* space those interfaces would be
    different.


    > I don't know whether my method is a common way. or there's some other
    > ways to do it.


    Hint: try to store the int, such that the result doesn't depend on int
    byte order (big endian & little endian).

    --
    Tor <torust [at] online [dot] no>
     
    Tor Rustad, Sep 11, 2007
    #12
  13. In article <>,
    Heinrich Pumpernickel <> wrote:
    >On Sep 11, 3:08 pm, (Kenny McCormack)
    >wrote:
    >
    >> Off topic. Not portable. Cant discuss it here. Blah, blah, blah.

    >
    >
    >why does he say that so often ? is he a bot ???
    >


    I say it far less often than, e.g., Keith Thompson, says it.

    And I say it a much more entertaining way, to boot.
     
    Kenny McCormack, Sep 11, 2007
    #13
  14. Yin Zhu

    Yin Zhu Guest

    On Sep 12, 1:37 am, Tor Rustad <> wrote:
    > Yin Zhu wrote:
    > > I am doing my operating system homework. I know how to use a buffer
    > > string to do read and write.

    >
    > Huh.. is this really OS homework?!

    actually not. but if I mention IO-efficient data structures, more
    people will be confused:(

    >
    > > But how about 32-bit int?

    >
    > > Here is my method to do:

    >
    > > typedef unsigned char *byte_pointer;
    > > write(fd, (byte_pointer)&i, sizeof(int));
    > > read(fd, (byte_pointer)&j, sizeof(int));

    >
    > > it works.

    >
    > It shouldn't, unless your code runs in *user* space. write() and read()
    > are POSIX system calls, but in *kernel* space those interfaces would be
    > different.
    >
    > > I don't know whether my method is a common way. or there's some other
    > > ways to do it.

    >
    > Hint: try to store the int, such that the result doesn't depend on int
    > byte order (big endian & little endian).
    >
    > --
    > Tor <torust [at] online [dot] no>
     
    Yin Zhu, Sep 12, 2007
    #14
  15. Yin Zhu

    Stan Milam Guest

    Yin Zhu wrote:
    > Hello, All
    >
    > Maybe this question is not proper to post here:( but anyway would
    > someone help me?
    >
    > I am doing my operating system homework. I know how to use a buffer
    > string to do read and write.
    >
    > But how about 32-bit int?
    >
    > Here is my method to do:
    >
    > typedef unsigned char *byte_pointer;
    > write(fd, (byte_pointer)&i, sizeof(int));
    > read(fd, (byte_pointer)&j, sizeof(int));
    >
    > it works.
    >
    > I don't know whether my method is a common way. or there's some other
    > ways to do it.
    >
    >
    > thanks in advance.
    >


    Just one thing: with some systems an int is 64 bits. So, I would
    include <stdint.h> and use sizeof(int32_t).

    Regards,
    Stan Milam
    --
    Life simultaneously, and paradoxically, makes indurate and pliable the soul.
     
    Stan Milam, Sep 12, 2007
    #15
  16. Yin Zhu

    Richard Bos Guest

    Heinrich Pumpernickel <> wrote:

    > On Sep 11, 3:08 pm, (Kenny McCormack)
    > wrote:
    >
    > > Off topic. Not portable. Cant discuss it here. Blah, blah, blah.

    >
    > why does he say that so often ? is he a bot ???


    Yes. Possibly one made of flesh and blood, but a bot all the same.

    Richard
     
    Richard Bos, Sep 12, 2007
    #16
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dan
    Replies:
    2
    Views:
    559
  2. Replies:
    3
    Views:
    513
    NOBODY
    Nov 12, 2005
  3. MangoMan
    Replies:
    1
    Views:
    407
    Paul Rubin
    Oct 11, 2003
  4. Peter Hartmann

    32bit install on 64bit system

    Peter Hartmann, Sep 15, 2005, in forum: Python
    Replies:
    1
    Views:
    405
    Christophe
    Sep 15, 2005
  5. miamia
    Replies:
    1
    Views:
    357
    Andrew Berg
    Jun 24, 2011
Loading...

Share This Page