Кто-нибудь знает, как читать в файле с необработанным кодированием? Так что... Я пытаюсь читать в поплавках или в двухлокальных (я думаю). Я застрял на этом несколько недель. Спасибо!
Файл, который я пытаюсь прочитать: http://www.sci.utah.edu/~gk/DTI-data/gk2/gk2-rcc-mask.raw
Описание raw-кодировки: hello://teem.sourceforge.net/nrrd/format.html#encoding (поменять привет на http для перехода на страницу) - "raw" - данные отображаются на диске точно так же, как в памяти, в условия байтовых значений и порядок байтов. Производится write() и fwrite(), подходящим для read() или fread().
Информация о файле: http://www.sci.utah.edu/~gk/DTI-data/gk2/gk2-rcc-mask.nhdr - Я думаю, что единственное, что имеет значение здесь, - большой эндиан (все еще пытающийся понять что это значит из Google) и необработанное кодирование.
Мой нынешний подход, неопределенный, если он правильный:
//Function ripped off from example of c++ ifstream::read reference page
void scantensor(string filename){
ifstream tdata(filename, ifstream::binary); // not sure if I should put ifstream::binary here
// other things I tried
// ifstream tdata(filename) ifstream tdata(filename, ios::in)
if(tdata){
tdata.seekg(0, tdata.end);
int length = tdata.tellg();
tdata.seekg(0, tdata.beg);
char* buffer = new char[length];
tdata.read(buffer, length);
tdata.close();
double* d;
d = (double*) buffer;
} else cerr << "failed" << endl;
}
/* P.S. I attempted to print the first 100 elements of the array.
Then I print 100 other elements at some arbitrary array indices (i.e. 9,900 - 10,000). I actually kept increasing the number of 0 until I ran out of bound at 100,000,000 (I don't think that how it works lol but I was just playing around to see what happens)
Here the part that makes me suspicious: so the ifstream different has different constructors like the ones I tried above.
the first 100 values are always the same.
if I use ifstream::binary, then I get some values for the 100 arbitrary printing
if I use the other two options, then I get -6.27744e+066 for all 100 of them
So for now I am going to assume that ifstream::binary is the correct one. The thing is, I am not sure if the file I provided is how binary files actually look like. I am also unsure if these are the actual numbers that I am supposed to read in or just casting gone wrong. I do realize that my casting from char* to double* can be unsafe, and I got that from one of the threads.
*/
Я очень ценю это!
Редактирование 1: прямо сейчас данные, считываемые с использованием вышеуказанного метода, являются, по-видимому, "неправильными", поскольку в парагенезе значения:
Dxx,Dxy,Dxz,Dyy,Dyz,Dzz
[0, 1], [-15.4006, 13.2248], [-5.32436, 5.39517], [-5.32915, 5.96026], [-17.87, 19.0954], [-6.02961, 5.24771], [-13.9861, 14.0524]
It a 3 x 3 symmetric matrix, so 7 distinct values, 7 ranges of values.
Поплавки, которые я сейчас обрабатываю из файла прямо сейчас, очень велики (т.е. -4.68855e-229, -1.32351e + 120).
Возможно, кто-то знает, как извлечь поплавки из Paraview?
Поскольку вы хотите работать с удвоениями, я рекомендую читать данные из файла в виде буфера удвоений:
const long machineMemory = 0x40000000; // 1 GB
FILE* file = fopen("c:\\data.bin", "rb");
if (file)
{
int size = machineMemory / sizeof(double);
if (size > 0)
{
double* data = new double[size];
int read(0);
while (read = fread(data, sizeof(double), size, file))
{
// Process data here (read = number of doubles)
}
delete [] data;
}
fclose(file);
}