c语言中读取内存的文件,c++从内存中读取文件内容,内容写到内存 实现文件的内存共享代码实例...
使用c++代碼進行內(nèi)存共享操作,內(nèi)存共享可以通過key value的形式來保存內(nèi)存,后面可以使用key值來直接讀取內(nèi)存,效率會很高/
函數(shù)說明:
shmget(key_t key, size_t size, int shmflg)(得到一個共享內(nèi)存標識符或創(chuàng)建一個共享內(nèi)存對象)
shmflg參數(shù)為模式標志參數(shù),使用時需要與IPC對象存取權限(如0600)進行|運算來確定信號量集的存取權限
shmat(int shmid, const void *shmaddr, int shmflg)(把共享內(nèi)存區(qū)對象映射到調(diào)用進程的地址空間)
shmdt(const void *shmaddr)(斷開共享內(nèi)存連接)
shmctl(int shmid, int cmd, struct shmid_ds *buf)(共享內(nèi)存管理)
cmd
IPC_RMID:刪除這片共享內(nèi)存
把內(nèi)容寫到內(nèi)存代碼:
write.cpp文件:
#include
//#include
//#include
//#include
//#include
#include
#include
//#include
//#include
//#include
//#include
//#include
//#include
//#include
#include
#include
#include
//#include
//#include
//#include
using namespace std;
typedef struct{
char name[8];
int age;
} people;
#define IPCKEY 0x11
#define SHMNAME "shm_ram"
#define OPEN_FLAG O_RDWR|O_CREAT
#define OPEN_MODE 00777
#define FILE_SIZE 4096 * 4
#define LINE_SIZE 1024 * 3
int main() {
//char buff[LINE_SIZE];
//
//FILE* fp = fopen("sightline.xml", "r");
//if (NULL == fp) {
//cout << "open failure" << endl;
//return 0;
//}
//
//while(!feof(fp)){
//memset(buff, 0, LINE_SIZE);
//fgets(buff, 1024, fp);
//cout << buff;
//}
//
//fclose(fp);
cout << "Content-type: text/html; charset=\"utf-8\" \n\n";
int shm_id, i;
key_t key;
char temp[8];
people *p_map;
char pathname[30];
strcpy(pathname, ".");
key = ftok(pathname, 0);
if (key == -1) {
//perror("ftok error");
cout << "ftok error" << endl;
return 0;
}
cout << "key=" << key << endl;
//printf("key=%d\n", key);
shm_id = shmget(key, 1024 * 4, IPC_CREAT | 0666);
if (shm_id == -1) {
//perror("shmget error");
cout << "shmget error" << endl;
return 0;
}
//delete
shmctl(shm_id, IPC_RMID, NULL) ;
shm_id = shmget(key, 1024 * 4, IPC_CREAT | 0666);
if (shm_id == -1) {
//perror("shmget error");
cout << "shmget error" << endl;
return 0;
}
cout << "shm_id=" << shm_id << endl;
//printf("shm_id=%d\n", shm_id);
p_map = (people*)shmat(shm_id, NULL, 0);
memset(temp, 0x00, sizeof(temp));
strcpy(temp, "test");
temp[4] = '0';
for (i = 0; i < 7; i++) {
temp[4] += 1;
strncpy((p_map + i)->name, temp, 5);
(p_map + i)->age = 0 + i + 1;
}
shmdt(p_map);
cout << "success" << endl;
//int shm_id, i;
//key_t key;
//people *p_map;
char pathname[30];
strcpy(pathname, "/tmp");
//key = ftok("../test", IPCKEY);
//if (key == -1) {
cout << "ftok error");
//cout << "ftok error" << endl;
//return -1;
//}
printf("key=%d\n", key);
//cout << "key=" << key << endl;
//shm_id = shmget(key, 0, 0);
//if (shm_id == -1) {
cout << "shmget error");
//cout << "shmget error" << endl;
//return -1;
//}
printf("shm_id=%d\n", shm_id);
//cout << "shm_id=" << shm_id << endl;
//p_map = (people*) shmat(shm_id, NULL, 0);
//for (i = 0; i < 3; i++) {
printf("name:%s\n", (*(p_map + i)).name);
printf("age %d\n", (*(p_map + i)).age);
//cout << "name=" << (*(p_map + i)).name << endl;
//cout << "age=" << (*(p_map + i)).age << endl;
//}
//if (shmdt(p_map) == -1) {
//cout << "detach error");
//return -1;
//}
return 0;
}
編譯? g++? write.cpp? -o write
執(zhí)行 ./write
read.cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
//#include
#include
#include
#include
#include
//#include
//#include
#include
#include
#include
#include
using namespace std;
typedef struct{
char name[8];
int age;
} people;
#define IPCKEY 0x11
#define SHMNAME "shm_ram"
#define OPEN_FLAG O_RDWR|O_CREAT
#define OPEN_MODE 00777
#define FILE_SIZE 4096 * 4
#define LINE_SIZE 1024 * 3
int main() {
//char buff[LINE_SIZE];
//
//FILE* fp = fopen("sightline.xml", "r");
//if (NULL == fp) {
//cout << "open failure" << endl;
//return 0;
//}
//
//while(!feof(fp)){
//memset(buff, 0, LINE_SIZE);
//fgets(buff, 1024, fp);
//cout << buff;
//}
//
//fclose(fp);
cout << "Content-type: text/html; charset=\"utf-8\" \n\n";
int shm_id, i;
key_t key;
people *p_map;
char pathname[30];
strcpy(pathname, ".");
key = ftok(pathname, 0);
if (key == -1) {
cout << "ftok error" << endl;
return 0;
}
cout << "key=" << key << endl;
shm_id = shmget(key, 0, 0);
if (shm_id == -1) {
cout << "shmget error" << endl;
return -1;
}
cout << "shm_id=" << shm_id << endl;
p_map = (people*) shmat(shm_id, NULL, 0);
for (i = 0; i < 3; i++) {
cout << "name=" << (*(p_map + i)).name << endl;
cout << "age=" << (*(p_map + i)).age << endl;
}
if (shmdt(p_map) == -1) {
cout << "detach error" << endl;
return -1;
}
return 0;
}
編譯? g++? read.cpp? -o read
執(zhí)行 ./read
總結(jié)
以上是生活随笔為你收集整理的c语言中读取内存的文件,c++从内存中读取文件内容,内容写到内存 实现文件的内存共享代码实例...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C语言已知二进制码求循环码,循环码系统与
- 下一篇: android 阻尼动画,Android