Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何复制以下场景?_C++_Visual C++_Shared Memory_Memcpy - Fatal编程技术网

C++ 如何复制以下场景?

C++ 如何复制以下场景?,c++,visual-c++,shared-memory,memcpy,C++,Visual C++,Shared Memory,Memcpy,我有一个内存映射文件和相应的映射视图文件句柄。内存映射文件将包含两部分数据: unsigned int指定实际数据的长度 实际相关数据 例如,如果我的实际数据长度为10个字符,则内存映射文件如下所示: 10 abcdefghij 其中abcdefghij是我想要的数据,并且会有一个换行符,其中包含长度编号和数据。如果需要,我也可以自由使用任何其他分隔符 现在我想先读取无符号int,以便能够读取实际数据。我正在考虑memcpy,但我不确定如何获得准确的无符号int值,因为我认为memcpy会逐字符

我有一个
内存映射文件
和相应的
映射视图文件
句柄。
内存映射文件将包含两部分数据:

  • unsigned int
    指定实际数据的长度
  • 实际相关数据
  • 例如,如果我的实际数据长度为
    10
    个字符,则内存映射文件如下所示:

    10
    abcdefghij

    其中
    abcdefghij
    是我想要的数据,并且会有一个换行符,其中包含长度编号和数据。如果需要,我也可以自由使用任何其他分隔符

    现在我想先读取
    无符号int
    ,以便能够读取实际数据。我正在考虑
    memcpy
    ,但我不确定如何获得准确的无符号int值,因为我认为
    memcpy
    会逐字符复制。如何从内容中提取
    10

    #include "windows.h"
    #include <iostream>
    
    int main() {
        const size_t buffer_size = 4098;
        const char* name = "my_shared_memory"; //sm name
        HANDLE file = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, buffer_size, name);
        LPVOID region = MapViewOfFile(file, FILE_MAP_ALL_ACCESS, 0, 0, buffer_size);
    
        const char* const msg = "this is my data"; //this is the string you want to pass
        const int msg_size = strlen(msg) + 1; //this is the size of the string data with \0
        const int total_size = msg_size + sizeof(int); //the total size of your data: the size of the string and the string by itself
        char* const data = (char*)malloc(total_size); //get some memory to place this data
        ((int*)data)[0] = msg_size; //set the first part of the data with the string msg size
        memcpy(data + sizeof(int), msg, msg_size); //set the msg by it self just after the size
    
        CopyMemory(region, data, total_size); //put on sm
    
        const int retrieved_size = ((int*)region)[0]; //retrieves the size of the msg from the sm
        char* const retrieved_msg = (char*)malloc(retrieved_size); //alloc some space for receive the msg with the retrieved size
        memcpy(retrieved_msg, (char*)region + sizeof(int), retrieved_size); //set the msg in the previous allocated memory
        std::cout << retrieved_msg << std::endl; //prints the msg
    
        free(data);
        free(retrieved_msg);
    }
    

    当您说数据以无符号整数开头时,您是指ascii编码的数字,而不是大或小的尾数整数值?但是,如果长度后的数据以一个数字开始,你怎么区分呢?@jdehesa绝对正确。如果您可以控制文件,我建议指定“文件的前x字节是数据的长度”或“ascii编码的数字后面跟一个换行符”。此内存映射文件的源将是另一个程序,该程序将仅将字符串作为字节数组复制到共享内存中。您能保证实际数据不会以数字字符开头吗?不,我不能保证,当文件格式指定数字为ascii-encoded.Works完全可以,但我不确定@JoshWilson打算做什么。该程序按照我的要求运行。如果您完全获得上面的代码,并将
    “这是我的数据”
    替换为
    “{1.3.4.5.10.1.1:[确定,字符串]}”
    ,则该程序将正常运行。还用以数字开头的字符串检查了上面的代码,并且也成功了。在设置数据缓冲区时,检查您是否移动了正确的大小。随机数可能是一些内存垃圾。检查在处理字符串时是否考虑了
    \0
    字符。问题是在内存块的正确位置没有获得正确的大小。请使用visual studio内存窗口帮助您检查数据缓冲区。
    const int max_digits_size = 3;
    char number[max_digits_size + 1];
    sprintf(number, "%d", msg_size);
    //your total_size is now (max_digits_size + 1) + msg_size
    
    //first you retrieve from sm the number as string in number_memcopied var, then you convert:
    const int retrieved_size = strtol(number_memcopied, NULL, 10);