/*
* =====================================================================================
*
* Filename: file_map.cpp
*
* Description:
*
* Version: 1.0
* Created: 2012年07月19日 10时08分07秒
* Revision: none
* Compiler: gcc
*
* Author: YOUR NAME (),
* Company:
*
* =====================================================================================
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
const char text[]="hello,world !";
int main(void){
int fd;
struct stat buf;
int rc;
void *fdstart;
fd=open("./log.bin",O_CREAT|O_RDWR,777);
if(fd<0){
printf("open log.bin fail\n");
}
if(truncate("./log.bin",1024) < 0){
cout<< "truncate error !"<
}
rc=fstat(fd,&buf);
if(rc == -1){
printf("File Size %d \n",buf.st_size);
}
cout << "buf.st_size %d "<
if((fdstart=mmap(NULL,buf.st_size,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0))==MAP_FAILED){
cout<<"mmap error!"<
close(fd);
exit(0);
}
cout <<"text size %d "<< sizeof(text)<
memcpy(fdstart,(void *)text,sizeof(text));
munmap(fdstart,buf.st_size);
close(fd);
return EXIT_SUCCESS;
}
编译后运行结果如下:
[root@localhost epoll]# ./mmp
buf.st_size %d 1024
text size %d 14
[root@localhost epoll]# cat log.bin
hello,world ![root@localhost epoll]# gedit file_map.cpp
[root@localhost epoll]#
阅读(1476) | 评论(0) | 转发(1) |