C/UNIX mmap int 数组

标签 c unix int mmap

是否可以将充满整数的mmap文件作为整数数组?我的意思是这样的(这是行不通的)

给定文件 tmp.in

1 2 15 1258

和类似的代码

int fd;
if ((fd = open("tmp.in", O_RDWR, 0666)) == -1)  {
    err(1, "open");
}

size_t size =  4 * sizeof(int);
int * map = (int *) mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);

我想打电话

printf("%d\n", map[2]+1);

预期结果

16

我发现字符映射使用 sscanf 来解析整数,但我需要数组中有数字(并且可能更改它们并在 munmap 时保存它们)。仅使用 POSIX 函数和系统调用。

我在这里找到了某物mmap reading stale array of integers from an file但我需要文件保持可读性(因此没有二进制表示)。使用 strtokatoi 迫使我使用另一个数组,不是吗?最后,我需要将我的数据复制回 map

感谢您的帮助:)

最佳答案

假设输入文件是文本文件(不是二进制文件,它可以有整数或任何二进制数据写入其中),您的文件将被映射为长度等于文件大小的字符串。

一旦这个字符串被映射到内存中,您就可以使用指针访问各个字符。

I'd like to be able call printf("%d\n", map[2]+1);

map [2]+1

这只会增加字符的 ASCII 值。

根据我的理解,您想将文件映射到内存中并更改整数等值。 只要文件是文本文件,这是不可能的。

我建议您将文件映射到内存中,读取字符,进行解析(在您的情况下查找空格)并更改字符值。

这里有一个示例代码:

[root@mohitsingh memoryMap]# cat sample.txt

12345

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>

#define FILEPATH "./sample.txt"
#define NUMINTS  (5)
#define FILESIZE (NUMINTS * sizeof(int))

int main(int argc, char *argv[])
{
                int i;
                int fd;
                char *map;  /* mmapped array of char */

                fd = open(FILEPATH, O_RDWR);
                if (fd == -1) {
                                                perror("Error opening file for reading");
                                                exit(EXIT_FAILURE);
                }

                map = mmap(0, FILESIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
                if (map == MAP_FAILED) {
                                                close(fd);
                                                perror("Error mmapping the file");
                                                exit(EXIT_FAILURE);
                }

                /* Read the file char-by-char from the mmap
                 **/
                for (i = 0; i <NUMINTS; ++i) {
                                                printf("%d: %c\n", i, map[i]);
                }
                /*change the character value
                *Implement your own logic here to change the values as integer
                */
                map[2]='9';
                if (munmap(map, FILESIZE) == -1) {
                                                perror("Error un-mmapping the file");
                }
                close(fd);
                return 0;
}

[root@mohitsingh memoryMap]# gcc test.c

[root@mohitsingh memoryMap]# ./a.out

0:1

1:2

2:3

3:4

4:5

[root@mohitsingh memoryMap]# cat sample.txt

12945

关于C/UNIX mmap int 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21254270/

相关文章:

linux - ssh公钥认证,无需更改系统文件

c - 接受系统调用时出现段错误

c - 从文件中读取 3x3 矩阵,然后在 C 中显示它

c - 功能类似于 SYSTEM(const char *) 且不采用 const char 但采用动态生成的查询的函数是什么

bash - 将完整的 make 输出重定向到文件

linux - 使用 sed 提取子表达式

c - 显示打印运行时错误

c - 如何动态分配二维数组,为什么?

c++ - std::string 部分转换为整数

Python - 如果进行了无效输入,则返回到函数的开头?