Guest User

Untitled

a guest
Feb 23rd, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. dim = size(matrix_in);
  2. N = max(size(dim));
  3.  
  4. fileID = fopen(file_out,'w');
  5. fwrite(fileID,N,'double'); % number of dimensions, usually 2
  6. fwrite(fileID,dim,'double'); % [nrows, ncols, ..]
  7. fwrite(fileID,matrix_in,'double');
  8. fclose(fileID);
  9.  
  10. #include <cstdio>
  11. #include <cstdlib>
  12.  
  13. int main(int argc, char *argv[])
  14. {
  15.  
  16. const char *fname = argv[1];
  17. FILE * myPtr;
  18. myPtr = fopen(fname, "r");
  19. // Test if input file exists, abort if it does not:
  20. if (!myPtr) {
  21. printf("nUnable to open file: > %s <, are you sure it exists? Aborting program.n", fname);
  22. return -1;
  23. }
  24.  
  25. /* Read in values from binary file here
  26. Format:
  27. - First byte: N = number of dimensions of matrix (bytes 1:8)
  28. - N*8 next bytes: dim = extent of matrix in all N dimensions (bytes 9:(n+1)*8)
  29. - prod(dim)*8 next bytes: A = values of matrix (column-wise) (8 bytes per value)
  30. */
  31.  
  32. // Read N:
  33. double n;
  34. fread(&n, sizeof(double), 1, myPtr);
  35. int N = (int)n;
  36. printf("The value of N = %dn", N); // This prints 2, as it should
  37.  
  38. // Read dim:
  39. double *dim = (double *)malloc(N * sizeof(double));
  40. fread(dim, sizeof(double), N, myPtr);
  41. printf("The values of dim are %f,%f.n", dim[0],dim[1]); // This prints 900.000000,522.000000, as it should
  42.  
  43. // Read A:
  44. int Nel = (int)(dim[0] * dim[1]); // Number of elements in A;
  45. double *A = (double *)malloc(Nel * sizeof(double)); // Allocate memory to store A;
  46. fread(A, sizeof(double), 150, myPtr); // Read in values to A;
  47. printf("There are %d elements in array A.n", Nel); // This prints 469800, as it should
  48.  
  49. // This is where the incorrectness shows:
  50. for (int i = 0; i < 136; i++) {
  51. printf("%d: %.8f: %p n", i, A[i],*A+i);
  52. }
  53.  
  54. // Close file, deallocate memory, and exit
  55. fclose(myPtr);
  56. free(dim);
  57. free(A);
  58. printf("End of program reached.n");
  59. return 0;
  60.  
  61. The value of N = 2
  62. The values of dim are 900.000000,522.000000.
  63. There are 469800 elements in array A.
  64. 0: -0.19632467: BFC9212AADC25959
  65. 1: -0.05302308: 3FE9B7B5548F69AA
  66. 2: 0.12151127: 3FFCDBDAAA47B4D5
  67. 3: -0.11154920: 40066DED5523DA6A
  68. // and so forth, until..
  69. 132: 0.11697594: 406079B7B5548F6A
  70. 133:-6277436239688796889934943738416506716724917685420501794480371793920.00: 406099B7B5548F6A
  71. 134:-6277438562204192487878988888393020692503707483087375482269988814848.00: 4060B9B7B5548F6A
  72. 135:-6277438562204192487878988888393020692503707483087375482269988814848.00: 4060D9B7B5548F6A
Add Comment
Please, Sign In to add comment