简体   繁体   English

如何从文本文件中读取带有mmap的双打数组

[英]How can I read an array of doubles with mmap from text file

I tried this: 我尝试了这个:

double *mat = (double *) mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED, fd, 0);

where sb.st_size is the size of the file and fd is the descriptor of the opened file. 其中sb.st_size是文件的大小,fd是打开的文件的描述符。 If I want to iterate through all the elements I get only 0s, but the file has only non 0 values. 如果要遍历所有元素,则只能得到0,但是文件只有非0值。 What is wrong with this? 这有什么问题?

You cannot. 你不能。

If you truly have a text file, then you have text numbers, almost like a spreadsheet: 如果您确实有一个文本文件,那么您就有文本编号,就像电子表格一样:

1.0 2.0 3.0
1.1 2.1 3.1

You cannot mmap that file and treat the mapped memory as double* , because that is character data. 你不能mmap那个文件,将映射的内存为double* ,因为那是字符数据。

In order to convert the file from character data to doubles, you will have to do something like fscanf or scanf to process the digits. 为了将文件从字符数据转换为双精度,您必须执行fscanfscanf来处理数字。

On the other hand, if you wrote out a blob of doubles as binary data, then you could do the mmap , pretty much as you describe. 另一方面,如果您将double的二进制数据写为二进制数据,则可以执行mmap ,正如您所描述的那样。

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

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