write() return value - Unix

This is a discussion on write() return value - Unix ; read() return: x > 0 on succes 0 if read EOF (on file read) or if the peer close connection (on socket read) -1 if an error occur. write() return: x>0 on succes -1 if an error occur Is possible ...

+ Reply to Thread
Results 1 to 5 of 5

Thread: write() return value

  1. write() return value

    read() return:
    x > 0 on succes
    0 if read EOF (on file read) or if the peer close connection (on
    socket read)
    -1 if an error occur.

    write() return:
    x>0 on succes
    -1 if an error occur
    Is possible that write() on file or socket return 0?
    thanks


  2. Re: write() return value

    gio wrote:
    > write() return:
    > x>0 on succes
    > -1 if an error occur
    > Is possible that write() on file or socket return 0?


    From a Linux man-page ...
    ....
    RETURN VALUE
    On success, the number of bytes written are
    returned (zero indicates nothing was written).
    On error, -1 is returned, and errno is set
    appropriately.
    ....
    So it seems that a return value of zero
    can happen.

    you might try
    ....
    rc=write(fd,buffer,0);
    printf("write(fd,buffer,0)=%d\n",rc);
    ....

    Regards ... Rainer


  3. Re: write() return value

    > So it seems that a return value of zero
    > can happen.
    >
    > you might try
    > ...
    > rc=write(fd,buffer,0);
    > printf("write(fd,buffer,0)=%d\n",rc);
    > ...
    >
    > Regards ... Rainer


    In my case I will never do something like:
    ....
    rc=write(fd,buffer,0);
    ....

    I will always do:
    ...
    rc=write(fd,buffer,dim); //dim > 0
    ....

    Is possible that in this case write return 0?


  4. Re: write() return value

    gio wrote:
    > Rainer Temme wrote:
    > > So it seems that a return value of zero
    > > can happen.
    > >
    > > you might try
    > > ...
    > > rc=write(fd,buffer,0);
    > > printf("write(fd,buffer,0)=%d\n",rc);


    > In my case I will never do something like:
    > ...
    > rc=write(fd,buffer,0);


    > I will always do:
    > ..
    > rc=write(fd,buffer,dim); //dim > 0


    > Is possible that in this case write return 0?


    I don't think so - the only occasion I could imagine this to happen
    would be for a write to something that was opened in non-blocking
    mode and where nothing could be written immediately. But then write()
    will return -1 and errno is set to EAGAIN. A return value of 0 now-
    adays seems to be reserved for the case that the number of bytes to
    be written is also 0, which can be used to check if write() would
    result in an error. But take care that this seems to a POSIX re-
    quirement while it looks as if historcal implementations did return
    0 when one tried to write() to something opened in non-blocking mode
    and nothing could be written immediately. See

    http://www.opengroup.org/onlinepubs/...ons/write.html

    for all the details.
    Regards, Jens
    --
    \ Jens Thoms Toerring ___ jt@toerring.de
    \__________________________ http://toerring.de

  5. Re: write() return value

    gio writes:
    > Is possible that write() on file or socket return 0?


    Yes. If the descriptor's O_NDELAY flag is set, a write() which
    would block returns 0. Which is why O_NONBLOCK is easier to deal
    with: With that flag it returns -1.

    Also write(,,size 0) returns 0, as someone mentioned.

    --
    Hallvard

+ Reply to Thread