Function Write()

This is a discussion on Function Write() within the C Programming forums, part of the General Programming Boards category; Hi, can anyone tell me how to get the retun value of the function: Code: write(int fildes, const void *buf, ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    37

    Function Write()

    Hi,

    can anyone tell me how to get the retun value of the function:

    Code:
    write(int fildes, const void *buf, size_t ubyte);
    Would I be right in saying that the return value of -1 for the function write represents an error?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    write should return a positive (or zero) value representing the number of bytes written for when it is successful, and anything negative returned is an error, yes. The value in errno would indicate WHAT went wrong.

    Edit: If the result of this:
    Code:
       if((bytes_written=write(handle, data, length)) != length)
          printf("SendData(): system call \"write()\" return error.\n  Asked for %d bytes to be written, but %d bytes reported as written.\n",
                 length,(int)bytes_written);
    is actually -1, then I'd say my reply in the thread with this code explains why - you should not be calling write with a handle from CreateFile or OpenFile.

    --
    Mats
    Last edited by matsp; 04-17-2009 at 05:27 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    37
    Thanks Mats

    but how would I find out the number of bytes it has written or if it returns an error?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by samuelmoneill View Post
    Thanks Mats

    but how would I find out the number of bytes it has written or if it returns an error?
    Erm, if it's negative, then it (most likely) didn't write any bytes. But what I was trying to say is that you are mixing functions that are part of different API's - write is not using the same type of information as a "handle" or "file descriptor" as you get back from CreateFile(). This is the source of the Warning you got yesterday, and whilst casting gets rid of the warning, it doesn't actually give the RIGHT VALUE that write would expect to use.

    WriteFile() will give you the number of bytes written as one of the parameters.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  4. Replies: 5
    Last Post: 02-08-2003, 07:42 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21