문제

Here is the result of 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            ............

It is simply a sequence of short int, each of size 2 bytes.

What I have in mind now is to create an arma::Mat<short>, then read in the file, two bytes at time, convert these two bytes to short int by bit switching, then assign it into the Mat.

This should work, but looks tedious, is there a better way?

도움이 되었습니까?

해결책 2

With hints from TonyWilk, I have come up with the following:

#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;
}

다른 팁

You could read the whole file into an array of short's. something like:

#define BUF_SIZE 12345;
short int buffer[BUF_SIZE];

FILE *fp= fopen("test.bin", "rb");
int nread= fread( (void *)buffer, 1, BUF_SIZE, fp );

Since I guess you are using C++, you can determine the filesize before reading and create the buffer dynamically. You'll still have to convert little/big endian individually tho.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top