简体   繁体   English

mmap从文件中读取过时的整数数组

[英]mmap reading stale array of integers from an file

I'm trying to read matrix of integers from file using mmap. 我正在尝试使用mmap从文件中读取整数矩阵。 If I receive it as char pointer from mmap function, I see everything correct but if I use int pointer, it gives me stale data. 如果我从mmap函数接收它作为char指针,我看到一切正确,但如果我使用int指针,它会给我陈旧的数据。 Problem with using char pointer is that I need to parse whole string using strtok or something else and get integers one by one. 使用char指针的问题是我需要使用strtok或其他东西解析整个字符串并逐个获取整数。 My matrix size is going to be 4k * 4k hence making that many calls to sscanf and strtok is not efficient. 我的矩阵大小将是4k * 4k,因此对sscanf和strtok的许多调用效率不高。 Please look at the program and output 请查看程序和输出

#define INTS 3 * 3

int main()
{

    FILE* in = fopen("int_file", "rb");
    int* ints = (int*)mmap(0, INTS * sizeof(int),
                      PROT_READ, MAP_FILE | MAP_PRIVATE, fileno(in),0);
    fclose(in);
    for(int i = 0; i < INTS; ++i) { 
        std::cout << ints[i] << std::endl;
    }
    munmap(ints, INTS * sizeof(int));
    return 0;
}

Contents of int_file is int_file的内容是

510 20 30 40 50 60 100 200 10000

Output 产量

540029237 857747506 808716848 540030240 822751286 84097028

The ACSII value of the text is getting printed. 正在打印文本的ACSII值。

Your text seems like: 你的文字看起来像:

510 20 30...

From ASCII table (to explain what I want to tell): 从ASCII表(解释我想说的):

No.       ASCII (hex)

Space ->   20
0     ->   30
1     ->   31
2     ->   32
3     ->   33
5     ->   35

int is 4 byte in size, so, taking first 4 bytes: int大小为4字节,因此,前4个字节:

Converting to ASCII, "510 " gives "35 31 30 20" in memory which is 0x20303135 ( 540029237 as decimal) for a little endian machine. 转换为ASCII, "510 "在内存中给出"35 31 30 20" ,对于小端机器,其为0x20303135 (小数为540029237 )。 Similarly, next 4 bytes "20 3" gives 0x33203032 ( 857747506 as decimal). 类似地,下一个4字节"20 3"给出0x33203032857747506为十进制)。 This is what you are getting. 这就是你得到的。

You need to convert each ACSII to integer using atoi() or similar, in this case. 在这种情况下,您需要使用atoi()或类似的方法将每个ACSII转换为整数。

But you may store your integers as their binary value itself rather than keeping it as ASCII. 但是您可以将整数存储为二进制值本身,而不是将其保存为ASCII。 The file will not be human readable but it will do your purpose. 该文件不是人类可读的,但它将达到您的目的。

Reading it as int * is possible if the data in file is stored from int array or continuous memory through calloc. 如果文件中的数据是通过calloc从int数组或连续内存中存储的,则可以将其读为int * Writer should look like, 作家应该看起来像,

#include <iostream>
#include <sys/mman.h>
#include <stdio.h>

using namespace std;

int inst[] = {510, 20, 30, 40, 50, 60, 100, 200, 10000 };

#define INTS 3 * 3

int main()
{
        FILE* out = fopen("int_file", "wb");  // Error checks are needed
        char *ptr = (char *) inst;
        fwrite( ptr, sizeof( int ), INTS, out );
        fclose( out);
        return 0;
}

Then the reader can read using mmap like as it is in your code, 然后读者可以像使用代码那样使用mmap进行阅读,

#include <iostream>
#include <sys/mman.h>
#include <stdio.h>

using namespace std;

#define INTS 3 * 3

int main()
{

    FILE* in = fopen("int_file", "rb");    // Error checks are needed
    int* ints = (int*)mmap(0, INTS * sizeof(int),
                    PROT_READ, MAP_FILE | MAP_PRIVATE, fileno(in),0);
    fclose(in);
    for(int i = 0; i < INTS; ++i) {
            std::cout << ints[i] << std::endl;
    }
    munmap(ints, INTS * sizeof(int));
    return 0;
}

If not stored from array or continuous memory, then using char * and strtok and atoi is the best solution.. 如果没有从数组或连续内存中存储,那么使用char *strtokatoi是最好的解决方案..

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

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