2013-01-15 86 views
7

我有一个问题需要将结构写入映射的内存文件。将结构写入映射的内存文件(mmap)

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

当我想写结构和阅读它,我不能去想,既然在mmap.write.c线32

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

和mmap.read.c线25

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

写入和读取整数/双/浮点/字符等没有区别,因为我可以把模式作为第二个参数“%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, "%d", &integer); 
     printf ("value: %d\n", integer); 
     usleep(100000); 
    } 
    //sprintf ((char*) file_memory, "%d\n", 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, 0, SEEK_SET); 
    /* Create the memory mapping. */ 
    file_memory = mmap (0, FILE_LENGTH, PROT_WRITE, MAP_SHARED, fd, 0); 
    close (fd); 
    /* Write a random integer to memory-mapped area. */ 
    for(i=0; i<10000; i++) { 
     sprintf((char*) file_memory, "%d\n", i); 
     //goto a; 
     usleep(100000); 
    } 
    a: 
    /* Release the memory (unnecessary because the program exits). */ 
    munmap (file_memory, FILE_LENGTH); 
    return 0; 
} 

感谢很多提前。

回答

10

首先你要跟踪其中在你要写的内存中,其次你必须记住映射的内存就像其他指向内存的指针一样。最后一位很重要,因为这意味着您可以使用普通数组索引来访问内存,或者使用诸如memcpy之类的函数将其复制到内存中。

自己写的一个结构,你有三种选择:

  1. 写结构原样,就像一个二进制文件。这意味着你必须将memcpy的结构移动到指定位置。

  2. 使用例如字段将结构逐字段地写为文本。 sprintf到正确的位置。

  3. 将内存视为一个大字符串,然后执行将每个字段的sprintf转换为临时缓冲区,然后将strcat添加到内存中。

+0

我喜欢第二种方式你提供的,简单而容易。由于我在嵌入式系统中使用它,因此不能保证有足够的空间。再次感谢你。 – totten

2

最简单的方法是只使用一个指针:

dataholder *dh = file_memory; 
/* now you can access dh->value, dh->time, dh->hash */ 

由于此结构不包含任何指针,如果你需要它或缩小复制,你可以为它分配,像:

dataholder dh_other = *dh; 

*dh = dh_other; 
相关问题