About binary matrix formats

Umut Tabak u.tabak at tudelft.nl
Thu Jul 30 14:52:37 CDT 2009


Dear all,

I was looking at the format on the MatLoad reference page, I would like 
to interface some matrices to petsc . To read them in matrix market 
format can take too long if the matrices are large. The 1st integer is 
binary file marker, MAT_FILE_COOKIE. When I read the file in Matlab with

fread(fid, 1, 'int')

I can get the value 1211216 which shows that it is a binary file. But 
doing the same in C++, I tried with the following code, does not give 
the true value. I know that reinterpret_cast can be quite implementation 
dependent. K33.bin is a matrix saved in binary format by Petsc.

    char c[4];
    char inname[] = "K33.bin";
    ifstream infile(inname, ios::binary);
    if (!infile){
      cout << "Couldn't open file " << inname << " for reading." << endl;
      return 1;
    }
    infile.read(c,4);
    int val;
    cout << (val = *(reinterpret_cast<int*>(c)));
   
If I write some numbers in binary format with some simple code, say 0 1 
2, I can convert them back to numerical values with this code, but I 
could not understand what happened  in the case of the binary file of 
Petsc. Should I do sth byte by byte? Most probably, this is due to my 
deficient programming knowledge, but clarification is appreciated.

Thanks and best regards,

Umut


More information about the petsc-users mailing list