微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将struct写入映射的内存文件(mmap)

将struct写入映射的内存文件时遇到问题.

我有两个文件,即mmap.write.c和mmap.read.c,在这文件中,我正在写一个整数到一个文件并从文件中读取它.

当我想编写struct并阅读它时,我无法想到这一点,因为在mmap.write.c的第32行

sprintf((char*) file_memory,"%d\n",i);

在mmap.read.c的第25行

sscanf (file_memory,"%d",&integer);

写入和读取integer / double / float / char等没有区别,因为我可以将pattern作为整数的第二个参数“%d”.但是我将在这里写下来表示结构?这是我的主要问题.

我想写和读的结构:

#define CHANNELS 20
typedef dataholder struct {
    int value[CHANNELS];
    time_t time;
    int hash;
}dataholder;

mmap.read.c

#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#include "mmap.h"
#define FILE_LENGTH 0x10000
int main (int argc,char* const argv[])
{
    int fd;
    void* file_memory;
    int integer;
    /* Open the file. */
    fd = open (argv[1],O_RDWR,S_IRUSR | S_IWUSR);
    printf("file opened\n");
    /* Create the memory mapping. */
    file_memory = mmap (0,FILE_LENGTH,PROT_READ | PROT_WRITE,MAP_SHARED,fd,0);
    printf("memfile opened\n");
    close (fd);
    printf("file closed\n");
    /* Read the integer,print it out,and double it. */
    while(1) {
        sscanf (file_memory,&integer);
        printf ("value: %d\n",integer);
        usleep(100000);
    }
    //sprintf ((char*) file_memory,2 * integer);
    /* Release the memory (unnecessary because the program exits). */
    munmap (file_memory,FILE_LENGTH);
    return 0;
}

mmap.write.c

#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#include "mmap.h"
#define FILE_LENGTH 0x10000
/* Return a uniformly random number in the range [low,high]. */
int random_range (unsigned const low,unsigned const high)
{
    unsigned const range = high - low + 1;
    return low + (int) (((double) range) * rand () / (RAND_MAX + 1.0));
}
int main (int argc,char* const argv[])
{
    int fd,i;
    void* file_memory;
    /* Seed the random number generator. */
    srand (time (NULL));
    /* Prepare a file large enough to hold an unsigned integer. */
    fd = open (argv[1],O_RDWR | O_CREAT | O_APPEND,S_IRUSR | S_IWUSR);
    //lseek (fd,FILE_LENGTH+1,SEEK_SET);
    write (fd,"",1);
    //lseek (fd,SEEK_SET);
    /* Create the memory mapping. */
    file_memory = mmap (0,PROT_WRITE,0);
    close (fd);
    /* Write a random integer to memory-mapped area. */
    for(i=0; i<10000; i++) {
        sprintf((char*) file_memory,i);
        //goto a;
        usleep(100000);
    }
    a:
    /* Release the memory (unnecessary because the program exits). */
    munmap (file_memory,FILE_LENGTH);
    return 0;
}

非常感谢提前.

解决方法

首先,你必须跟踪你想要写入内存的位置,其次你要记住映射的内存就像任何其他指向内存的指针一样.最后一点很重要,因为这意味着您可以使用普通数组索引来访问内存,或使用memcpy函数复制到内存中.

要编写结构,您有三种选择:

>按原样编写结构,就像在二进制文件中一样.这意味着您必须将结构memcpy到指定位置.>使用例如文本逐字段地写结构. sprintf到正确的位置.>将内存视为一个大字符串,例如,sprintf将每个字段放入一个临时缓冲区,然后strcat将其添加到内存中.

原文地址:https://www.jb51.cc/c/119621.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐