日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

c语言中读取内存的文件,c++从内存中读取文件内容,内容写到内存 实现文件的内存共享代码实例...

發布時間:2024/9/15 c/c++ 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c语言中读取内存的文件,c++从内存中读取文件内容,内容写到内存 实现文件的内存共享代码实例... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

使用c++代碼進行內存共享操作,內存共享可以通過key value的形式來保存內存,后面可以使用key值來直接讀取內存,效率會很高/

函數說明:

shmget(key_t key, size_t size, int shmflg)(得到一個共享內存標識符或創建一個共享內存對象)

shmflg參數為模式標志參數,使用時需要與IPC對象存取權限(如0600)進行|運算來確定信號量集的存取權限

shmat(int shmid, const void *shmaddr, int shmflg)(把共享內存區對象映射到調用進程的地址空間)

shmdt(const void *shmaddr)(斷開共享內存連接)

shmctl(int shmid, int cmd, struct shmid_ds *buf)(共享內存管理)

cmd

IPC_RMID:刪除這片共享內存

把內容寫到內存代碼:

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

執行 ./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

執行 ./read

總結

以上是生活随笔為你收集整理的c语言中读取内存的文件,c++从内存中读取文件内容,内容写到内存 实现文件的内存共享代码实例...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。