Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1120288
  • 博文数量: 177
  • 博客积分: 761
  • 博客等级: 上士
  • 技术积分: 1518
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-04 22:37
文章分类

全部博文(177)

文章存档

2017年(1)

2016年(3)

2015年(33)

2014年(48)

2013年(60)

2012年(32)

分类: C/C++

2012-07-19 13:30:40

/*
 * =====================================================================================
 *
 *       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]# 

阅读(1405) | 评论(0) | 转发(1) |
0

上一篇:没有了

下一篇:数组模拟循环队列

给主人留下些什么吧!~~