代码实例:
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
extern int errno;
#define ERR_EXIT(msg) \
do { \
fprintf(stderr, "[%s][%d] %s : %s\n",__FILE__,__LINE__,\
msg,strerror(errno)); \
exit(EXIT_FAILURE); \
}while ( 0 )
int main( void )
{
int fd = -1;
if ( -1 == (fd=open("test.txt", O_RDWR)) )
ERR_EXIT("open");
if ( ftruncate(fd,18695000000) == -1 )
ERR_EXIT("ftruncate");
lseek(fd, 0, SEEK_END);
write(fd, "!", 1);
close(fd);
}
以上代码:ftruncate改变文件大小为18G左右,在windows下看文件大小如下图所示,显示文件确实为改变之后的大小,而且拷贝的时候也是18G大小