Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1126559
  • 博文数量: 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-23 10:45:36

/*
 * =====================================================================================
 *
 *       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 !";
#define MAX_MSG_TOTAL 5

typedef struct MSG_INFO{
int   msg_total;
char msg_info[MAX_MSG_TOTAL][128];
}_MSG_INFO_T;

typedef struct MSG_QUEUE
{
_MSG_INFO_T log;
int front;//log queue front
int rear;//log queue rear
}_MSG_QUEUE_T;

void init_log_gueue(_MSG_QUEUE_T *plog);
bool is_msg_queue_full(_MSG_QUEUE_T *plog);
bool delete_log_from_queue(_MSG_QUEUE_T *plog);
bool add_log_to_msg_queue(_MSG_QUEUE_T *plog,char *string);
void print_log_queue(_MSG_QUEUE_T *plog);

int main(void){
int fd;
struct stat buf;
int rc;
void *fdstart;
int select;
char msg_log[128];
_MSG_QUEUE_T log_info;
init_log_gueue(&log_info);

fd=open("./log.bin",O_CREAT|O_RDWR,777);
if(fd<0){
printf("open log.bin fail\n");
}
if(truncate("./log.bin",sizeof(_MSG_QUEUE_T)) < 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)<
while(1){
cout<<"Please Input your chocie,and Ether!"<
scanf("%d",&select);
switch(select){
case 1:
cin>>msg_log;
add_log_to_msg_queue(&log_info,msg_log);
break;
case 2:
print_log_queue(&log_info);
break;
default:break;
}
//cin>>msg_log;
//add_log_to_msg_queue(&log_info);

}
memcpy(fdstart,(void *)text,sizeof(text));
munmap(fdstart,buf.st_size);
close(fd);
return EXIT_SUCCESS;
}



void init_log_gueue(_MSG_QUEUE_T *plog){
int i;
plog->front=0;
plog->rear=0;
plog->log.msg_total=0;
for(i=0;i
bzero(&plog->log.msg_info[i][0],sizeof(plog->log.msg_info[i]));
}
}

bool is_msg_queue_full(_MSG_QUEUE_T *plog){
if(plog->log.msg_total==MAX_MSG_TOTAL)
return true;
else
return false;
}

bool delete_log_from_queue(_MSG_QUEUE_T *plog){
if(plog->log.msg_total==0){
//no meseage for delete
return false;
}
if(plog->front < (MAX_MSG_TOTAL-1)){
bzero(&plog->log.msg_info[plog->front][0],sizeof(plog->log.msg_info[plog->front]));
plog->front++;
}else{
//the front is pointer the queue rear,so goto front array
bzero(&plog->log.msg_info[plog->front][0],sizeof(plog->log.msg_info[plog->front]));
plog->front=0;
}
plog->log.msg_total--;
return true;

}


bool add_log_to_msg_queue(_MSG_QUEUE_T *plog,char *logstring){

if(plog->log.msg_total==MAX_MSG_TOTAL){
//check queue is full ?
delete_log_from_queue(plog);
}

printf("add info::%s\n",logstring);
strcpy((char*)&plog->log.msg_info[plog->rear][0],logstring);
plog->rear++;
plog->log.msg_total++;
printf("front %d rear %d msg_total %d \n",plog->front,plog->rear,plog->log.msg_total);
if(plog->rear == MAX_MSG_TOTAL){//end the array
plog->rear=0;//goto back frist array eletment
}
}

void print_log_queue(_MSG_QUEUE_T *plog){
int i;
int front;
front=plog->front;

for(i=0;ilog.msg_total;i++){
if(front < MAX_MSG_TOTAL){
cout<<"log "<log.msg_info[front]<
front++;
}else{
front=0;
cout<<"log "<log.msg_info[front]<
front++;
}
}
}




阅读(1629) | 评论(2) | 转发(2) |
给主人留下些什么吧!~~

lubing5212012-08-02 14:11:28

内存映射和文件操作的应用和标题不冲突,只是在工程用到的时候采用。

3089389692012-07-25 16:15:00

晕,,,不知道你的程序中间的那个文件操作和内存映射是做什么用的,貌似你的数组队列也没有用到啊。