当前位置: 技术问答>linux和unix
请问一个pwrite函数的问题?
来源: 互联网 发布时间:2017-03-16
本文导语: 我在32位的linux下写了一个程序,程序代码如下: #define _FILE_OFFSET_BITS 64 #define __USE_FILE_OFFSET64 #define __USE_LARGEFIILE64 #define _LARGEFILE64_SOURCE #include #include #include #include #include #include int main() { int fd; int count = 128; ...
我在32位的linux下写了一个程序,程序代码如下:
#define _FILE_OFFSET_BITS 64
#define __USE_FILE_OFFSET64
#define __USE_LARGEFIILE64
#define _LARGEFILE64_SOURCE
#include
#include
#include
#include
#include
#include
int main()
{
int fd;
int count = 128;
off_t offset = 0X80000000;
int ret;
char buf[1024]="hi ! this is pwrite.";
char pathname[128] = "/tmp/1.txt";
fd = open( pathname, O_WRONLY);
printf("size of off_t is %dn",sizeof(off_t));
if((ret = pwrite(fd, buf, count, offset))==-1)
{
printf("errno = %dn",errno);
printf("pwrite errorn");
exit(1);
}
else
{
printf("pwrite successn");
printf("the writed data is:%s", buf);
}
return 0;
}
但是pwrite函数返回-1,errorno是22,但是我把 0X80000000设为0X7fffffff,就很正常。请问这是什么原因?如何解决这个问题?谢谢!
#define _FILE_OFFSET_BITS 64
#define __USE_FILE_OFFSET64
#define __USE_LARGEFIILE64
#define _LARGEFILE64_SOURCE
#include
#include
#include
#include
#include
#include
int main()
{
int fd;
int count = 128;
off_t offset = 0X80000000;
int ret;
char buf[1024]="hi ! this is pwrite.";
char pathname[128] = "/tmp/1.txt";
fd = open( pathname, O_WRONLY);
printf("size of off_t is %dn",sizeof(off_t));
if((ret = pwrite(fd, buf, count, offset))==-1)
{
printf("errno = %dn",errno);
printf("pwrite errorn");
exit(1);
}
else
{
printf("pwrite successn");
printf("the writed data is:%s", buf);
}
return 0;
}
但是pwrite函数返回-1,errorno是22,但是我把 0X80000000设为0X7fffffff,就很正常。请问这是什么原因?如何解决这个问题?谢谢!
|
某些条件下pwrite和pwrite64是一样的,如下红色
但是#if defined __USE_UNIX98 || defined __USE_XOPEN2K8这个条件不满足的话pwrite和pwrite64是不同的,pwrite只接收32位的off_t
#if defined __USE_UNIX98 || defined __USE_XOPEN2K8
# ifndef __USE_FILE_OFFSET64
/* Read NBYTES into BUF from FD at the given position OFFSET without
changing the file pointer. Return the number read, -1 for errors
or 0 for EOF.
This function is a cancellation point and therefore not marked with
__THROW. */
extern ssize_t pread (int __fd, void *__buf, size_t __nbytes,
__off_t __offset) __wur;
/* Write N bytes of BUF to FD at the given position OFFSET without
changing the file pointer. Return the number written, or -1.
This function is a cancellation point and therefore not marked with
__THROW. */
extern ssize_t pwrite (int __fd, __const void *__buf, size_t __n,
__off_t __offset) __wur;
# else
# ifdef __REDIRECT
extern ssize_t __REDIRECT (pread, (int __fd, void *__buf, size_t __nbytes,
__off64_t __offset),
pread64) __wur;
extern ssize_t __REDIRECT (pwrite, (int __fd, __const void *__buf,
size_t __nbytes, __off64_t __offset),
pwrite64) __wur;
# else
# define pread pread64
# define pwrite pwrite64
# endif
# endif
但是#if defined __USE_UNIX98 || defined __USE_XOPEN2K8这个条件不满足的话pwrite和pwrite64是不同的,pwrite只接收32位的off_t
#if defined __USE_UNIX98 || defined __USE_XOPEN2K8
# ifndef __USE_FILE_OFFSET64
/* Read NBYTES into BUF from FD at the given position OFFSET without
changing the file pointer. Return the number read, -1 for errors
or 0 for EOF.
This function is a cancellation point and therefore not marked with
__THROW. */
extern ssize_t pread (int __fd, void *__buf, size_t __nbytes,
__off_t __offset) __wur;
/* Write N bytes of BUF to FD at the given position OFFSET without
changing the file pointer. Return the number written, or -1.
This function is a cancellation point and therefore not marked with
__THROW. */
extern ssize_t pwrite (int __fd, __const void *__buf, size_t __n,
__off_t __offset) __wur;
# else
# ifdef __REDIRECT
extern ssize_t __REDIRECT (pread, (int __fd, void *__buf, size_t __nbytes,
__off64_t __offset),
pread64) __wur;
extern ssize_t __REDIRECT (pwrite, (int __fd, __const void *__buf,
size_t __nbytes, __off64_t __offset),
pwrite64) __wur;
# else
# define pread pread64
# define pwrite pwrite64
# endif
# endif