pread and pwrite not defined?
Asked Answered
C

1

5

I am trying to use pread and pwrite so that I can lseek to the beginning of the file and start reading or writing in one atomic operation. Both of these functions will do that for me however, the issue I am having is that the compiler is giving me warning: implicit declaration of function ‘pread’ even after I added #define _XOPEN_SOURCE 500 and #include<unistd.h> like the man pages said. Am I missing something? The two statements with the function calls are below. Thanks!

 #include<unistd.h>



 #define _XOPEN_SOURCE 500
    int main (int argc, char *argv[])
    {



while ( (read_in += pread(source_fd, in_buf, in_buf_size,read_in) )  )
    {

if (write_out += pwrite(dest_fd, in_buf, read_in, write_out) == -1)
        {
Cagliari answered 13/9, 2012 at 17:47 Comment(2)
Can you show the entire code?Superhuman
@Superhuman sure, I added all of the code as an edit. I thought that it might be too much code to do that.Cagliari
S
8

You need to define _XOPEN_SOURCE 500 before your includes:

#define _XOPEN_SOURCE 500

#include<unistd.h>

Otherwise, the unistd.h header won't see the macro definition.

Superhuman answered 13/9, 2012 at 17:50 Comment(2)
Thank you! Perhaps it would be useful for me to know what the _XOPEN_SOURCE 500 is for so that I will better understand it, would you happen to know what that is?Cagliari
@Cagliari If defined (with any value), expose POSIX.1, POSIX.2, and X/Open (XPG4) definitions. If defined with the value 500 or greater, also expose SUSv2 (UNIX 98 and XPG5) extensions.Cephalothorax

© 2022 - 2024 — McMap. All rights reserved.