这是xxd test.bin的结果
0000000: 0100 0200 0300 0400 0500 0600 0700 0800 ................
0000010: 0900 0a00 0200 0400 0600 0800 0a00 0c00 ................
0000020: 0e00 1000 1200 1400 0300 0600 0900 0c00 ................
0000030: 0f00 1200 1500 1800 1b00 1e00 ............
它只是一个简短的int序列,每个序列的大小为2个字节。
我现在想到的是创建一个arma::Mat<short>
,然后一次读取两个字节的文件,然后通过位切换将这两个字节转换为short int,然后将其分配给Mat。
这应该可行,但看起来很乏味,是否有更好的方法?
在TonyWilk的提示下,我提出了以下建议:
#include <sys/stat.h>
#include <stdint.h>
#include <armadillo>
int main()
{
struct stat statbuf;
stat("bigmat.bin", &statbuf);
uintmax_t fsize = (uintmax_t)statbuf.st_size;
uintmax_t bsize = fsize / sizeof(short);
short buffer[bsize];
FILE *fp = fopen("bigmat.bin", "rb");
fread((void*) buffer, 1, fsize, fp);
// for(uintmax_t i=0; i<bsize; i++) {
// printf("%hd \n", buffer[i]);
// }
arma::Mat<short> mymat(buffer, 10, 3, false);
std::cout << mymat << std::endl;
return 0;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句