简体   繁体   English

C ++从具有共享内存的文件中读取

[英]C++ Read from file with shared memory

I wrote 2 programs ,the first one make shared memory with two variables for the character and string size.And the other program is read the shared variables. 我编写了2个程序,第一个程序使用两个变量(用于字符和字符串大小)来共享内存。另一个程序读取共享变量。

My problem : I try to make my programs to read all words in the text file , but i can't to do it so i made my programs to read 1 word.How i can make it for all words in file. 我的问题:我试图使我的程序读取文本文件中的所有单词,但是我无法做到这一点,所以我使我的程序读取1个单词。如何使文件中的所有单词都可以读取它。

To read t he file, this region 为了阅读文件,该区域

if (file1.is_open())
{
     file1>> word;//read word from file to know size of word $$ read first char into shm ->str
     shm->size=word.length();
     strcpy(str2, word.c_str());
     shm ->str=str2[0];
     file1<<"             ";
 }

Should be more along the lines of 应该更符合

while(file1>> word)
{
     shm->size=word.length();
     shm ->str=word[0];
 }

I got rid of the strcpy as it didn't seem to be needed, and the file1<<" "; 我摆脱了strcpy因为似乎并不需要它,而file1<<" "; because only pain can come from trying to write to an ifstream. 因为尝试编写ifstream只会带来痛苦。 By default the file backed by ifstream has been opened for read only and cannot be written. 默认情况下,ifstream支持的文件已被打开,仅供只读,无法写入。 If it must be written use fstream and specify std::fstream::in | std::fstream::out 如果必须编写,则使用fstream并specify std::fstream::in | std::fstream::out specify std::fstream::in | std::fstream::out on the open call. specify std::fstream::in | std::fstream::out在公开通话中。 You will probably want to think carefully about how you intend to write to the file while you are reading it. 您可能需要仔细考虑在阅读文件时打算如何写入文件。

Currently shm will be overwritten by every word found. 当前,shm将被找到的每个单词覆盖。 This is probably not what you want. 这可能不是您想要的。 I suspect you're after something more like: 我怀疑您正在追求更像:

  1. open file 打开文件
  2. wait for turn 等待轮到
  3. read from file 从文件读取
  4. update shmem 更新shmem
  5. set other's turn 轮到别人了
  6. goto 2 转到2

Something like 就像是

sharedBoundary->turn==0

file1.open ("file1.txt");
while(file1>> word)
{
     while(sharedBoundary->turn==1);
     shm->size=word.length();
     shm ->str=word[0];
     sharedBoundary->turn=1;     
}

I don't see the point for both sharedBoundary flag and turn unless there is much more significant guard logic we aren't shown, so I dropped the flag to simplify the example. 除非没有显示更重要的后卫逻辑,否则我不会看到sharedBoundary标志和转向的意义,因此我删除了该标志以简化示例。 My usage of may be incorrect, so adapt to taste. 我对的使用可能不正确,因此要适应口味。

Program 2. In order to synchronize with Program 1, you need something like this: 程序2。为了与程序1同步,您需要以下内容:

sharedBoundary->turn==0
while(/* unspecified termination logic */)
{
    while(sharedBoundary->turn==0);
    cout<<"The new values stored in the shared memory:\n";
    cout<<"Text="<<shm ->str<<"\nSize="<<shm->size<<"\n";
    PrintCharNtimes(shm ->str, shm->size);
    sharedBoundary->turn=0;     
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM