Any problem with this code? - Unix

This is a discussion on Any problem with this code? - Unix ; Hi, The following code is as similar as the fragments showed in section 2.15, AUP. In the code, the version should be 1 but the output is 0. What's the problem? #include #include #include #include #include #define PERM_FILE ( S_IRUSR ...

+ Reply to Thread
Results 1 to 4 of 4

Thread: Any problem with this code?

  1. Any problem with this code?

    Hi,

    The following code is as similar as the fragments showed in section
    2.15, AUP. In the code, the version should be 1 but the output is 0.
    What's the problem?

    #include
    #include
    #include
    #include
    #include

    #define PERM_FILE ( S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH )

    int main( )
    {
    struct header {
    int h_version;
    int h_num_items;
    } hdr, *hp;

    struct data {
    enum {TYPE_STRING, TYPE_FLOAT} d_type;
    union {
    float d_val;
    char d_str[ 20 ];
    } d_data;
    } d1, d2, *dp;

    struct iovec v[ 3 ];

    hdr.h_version = 1;
    hdr.h_num_items = 2;
    d1.d_type = TYPE_STRING;
    strcpy( d1.d_data.d_str, "Hello!" );
    d2.d_type = TYPE_FLOAT;
    d2.d_data.d_val = 123.456;

    v[ 0 ].iov_base = ( char* )&hdr;
    v[ 0 ].iov_len = sizeof( hdr );
    v[ 1 ].iov_base = ( char* )&d1;
    v[ 1 ].iov_len = sizeof( d1 );
    v[ 2 ].iov_base = ( char* )&d2;
    v[ 2 ].iov_len = sizeof( d2 );

    int fd = open( "/home/tony/Program/temp/aaa", O_WRONLY | O_TRUNC,
    PERM_FILE );

    int n = writev( fd, v, sizeof( v ) / sizeof( v[ 0 ] ) );

    printf( "%d\n", n );

    char *buf;
    buf = malloc( n );
    lseek( fd, 0, SEEK_SET );
    read( fd, buf, n );
    hp = buf;
    dp = ( struct data* )( hp + 1 );

    printf( "Version= %d\n", hp->h_version );

    return 0;
    }

  2. Re: Any problem with this code?

    Tony, Zhang wrote:
    > Hi,
    >
    > The following code is as similar as the fragments showed in section
    > 2.15, AUP. In the code, the version should be 1 but the output is 0.
    > What's the problem?
    >
    > #include
    > #include
    > #include
    > #include
    > #include
    >
    > #define PERM_FILE ( S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH )
    >
    > int main( )
    > {
    > struct header {
    > int h_version;
    > int h_num_items;
    > } hdr, *hp;
    >
    > struct data {
    > enum {TYPE_STRING, TYPE_FLOAT} d_type;
    > union {
    > float d_val;
    > char d_str[ 20 ];
    > } d_data;
    > } d1, d2, *dp;
    >
    > struct iovec v[ 3 ];
    >
    > hdr.h_version = 1;
    > hdr.h_num_items = 2;
    > d1.d_type = TYPE_STRING;
    > strcpy( d1.d_data.d_str, "Hello!" );
    > d2.d_type = TYPE_FLOAT;
    > d2.d_data.d_val = 123.456;
    >
    > v[ 0 ].iov_base = ( char* )&hdr;
    > v[ 0 ].iov_len = sizeof( hdr );
    > v[ 1 ].iov_base = ( char* )&d1;
    > v[ 1 ].iov_len = sizeof( d1 );
    > v[ 2 ].iov_base = ( char* )&d2;
    > v[ 2 ].iov_len = sizeof( d2 );
    >
    > int fd = open( "/home/tony/Program/temp/aaa", O_WRONLY | O_TRUNC,
    > PERM_FILE );

    That means open only for writing
    >
    > int n = writev( fd, v, sizeof( v ) / sizeof( v[ 0 ] ) );
    >
    > printf( "%d\n", n );
    >
    > char *buf;
    > buf = malloc( n );
    > lseek( fd, 0, SEEK_SET );
    > read( fd, buf, n );

    This call should fail. What does it return?

    Robert

    > hp = buf;
    > dp = ( struct data* )( hp + 1 );
    >
    > printf( "Version= %d\n", hp->h_version );
    >
    > return 0;
    > }


  3. Re: Any problem with this code?

    "Tony, Zhang" writes:

    > In the code, the version should be 1 but the output is
    > 0. What's the problem?


    Several. You should check return values from malloc(), lseek(),
    read(), etc. If you did, you'd find out that your read() fails
    (hint: what do you think O_WRONLY means?).

    Also, on Linux your program crashes, unless 'aaa' file already
    exists (hint: perhaps you want to add O_CREAT?).

    Cheers,
    --
    In order to understand recursion you must first understand recursion.
    Remove /-nsp/ for email.

  4. Re: Any problem with this code?

    I see.

    I have made a foolish mistake!

    Thanks a lot! Thanks for your guys very much!

    Best,
    Tony

    Paul Pluzhnikov wrote:
    > "Tony, Zhang" writes:
    >
    >> In the code, the version should be 1 but the output is
    >> 0. What's the problem?

    >
    > Several. You should check return values from malloc(), lseek(),
    > read(), etc. If you did, you'd find out that your read() fails
    > (hint: what do you think O_WRONLY means?).
    >
    > Also, on Linux your program crashes, unless 'aaa' file already
    > exists (hint: perhaps you want to add O_CREAT?).
    >
    > Cheers,


+ Reply to Thread