如何解决将数据加载到共享内存中
我想知道如何将文本文件中的数据加载到C或C ++中的共享内存中。我想逐行将文件的内容分配到结构中,例如:
name: gaimi dancer id: 443432 address: 123 Southbrook Dr. Mombai,In PhoneNumber: 8876549300
以此类推,然后将这些结构加载到共享内存中。
我已经尝试了一切,但没有任何效果。我已经尝试过strcpy()
和memcpy()
,但它们没有用。
reader.cpp
#include <sys/shm.h>
#include <stdio.h>
#include<string>
#include<cstring>
#include<cstdlib>
using namespace std;
struct Stu_Info{
char name[30];
char id[20];
char address[30];
char PhoneNumber[20];
};
int main()
{
// ftok for generating unique key
key_t key = ftok("shm",6556);
// shmget returns an identifier in shmid
id = shmget(key,1024*4,0666|IPC_CREAT);
//to make sure the shm made correctly
if (id < 0){
perror("create: shmget failed");
exit(1);
}else{
cout << "The shm was craeted!!";
}
// shmat to attach to shared memory
char *str = (char*) shmat(id,(void*)0,0);
cout<<"Write Data : ";
gets(str);
printf("Data written in memory: %s\n",str);
//detach from shared memory
shmdt(str);
return 0;
}
writer.cpp
#include <iostream>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
using namespace std;
int main()
{
// ftok to generate unique key
key_t key = ftok("shmfile",65);
// shmget returns an identifier in shmid
int id = shmget(key,1024,0666|IPC_CREAT);
// shmat to attach to shared memory
char *str = (char*) shmat(id,0);
printf("Data read from memory: %s\n",str);
//detach from shared memory
shmdt(str);
// destroy the shared memory
shmctl(id,IPC_RMID,NULL);
return 0;
}
text.txt
gaimi dancer 443432 123 Southbrook Dr. Mombai,In 8876549300 Yall S Player 465387 543 Saif Rd. Manama,BA 7665647833 Dean Rama 456339 908 Sea Ave. Manama,BA 4556748900 Maya Rab 677032 654 South 3rd St. Kwait,KW 3425435456 Patricia Carey 547839 320 Old navy St. Chiacgo,IL 1112332534
解决方法
我在Mac命令行上运行cpp,我需要使用这种方式来执行此操作,并且还将使用信号量方式。我已经试过了这段代码,但是没有用。
while (getline(myFile,line)) //getline function to get the data line by line
{
// cout << line << "\n"; // to print the content of the file
for(int j=0;j<=15;j++){//to create 15 student structs
for (int i=0; i<=15;i+4){ // to iterate in every 4 lines for 15 student strcts to save tha name.
strcpy(student[j][i].name,line);
name = student[j][i].name;
// cout <<student[j][i].name<<"\n";
memcpy(str,name,strlen(name)); // to load tha data into shared memory
}
for (int i=1; i<=15;i+4){ // to iterate in every 4 lines for 15 student strcts to save the id.
strcpy(student[j][i].id,line);
id = student[j][i].id;
memcpy(str,id,strlen(id)); // to load tha data into shared memory
}
for (int i=2; i<=15;i+4){ // to iterate in every 4 lines for 15 student strcts to save the address.
strcpy(student[j][i].address,line);
address = student[j][i].address;
memcpy(str,address,strlen(address)); // to load tha data into shared memory
}
for (int i=3; i<=15;i+4){ // to iterate in every 4 lines for 15 student strcts to save phone number.
strcpy(student[j][i].PhoneNumber,line);
PhoneNumber = student[j][i].PhoneNumber;
memcpy(str,PhoneNumber,strlen(PhoneNumber)); // to load tha data into shared memory
}
}
}
myFile.close();
} else{cout << "unable to open a file!";}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。