Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pread and pwrite not defined?

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)
        {
like image 342
tpar44 Avatar asked Sep 13 '12 17:09

tpar44


People also ask

What is Pread and Pwrite?

pread() reads up to count bytes from file descriptor fd at offset offset (from the start of the file) into the buffer starting at buf. The file offset is not changed. pwrite() writes up to count bytes from the buffer starting at buf to the file descriptor fd at offset offset. The file offset is not changed.

What is the difference between read and Pread?

The pread() function performs the same action as read(), except that it reads from a given position in the file without changing the file pointer. The first three arguments to pread() are the same as read(), with the addition of a fourth argument offset for the desired position inside the file.


1 Answers

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.

like image 177
Mysticial Avatar answered Nov 02 '22 20:11

Mysticial