-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathprint.cpp
65 lines (57 loc) · 1.65 KB
/
print.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/**
* Matrix Print Program
* by Cody Rivera, 2019
*
* Usage - ./print [-d] file
*/
#define COL_WIDTH 8
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
template <typename FloatType>
void printMatrix(FloatType *A, unsigned int m, unsigned int n) {
std::cout << std::setw(COL_WIDTH) << m << " " << std::setw(COL_WIDTH) << n
<< std::endl
<< std::endl;
for (unsigned int i = 0; i < m; i++) {
for (unsigned int j = 0; j < n; j++) {
std::cout << std::setw(COL_WIDTH) << A[i + (j * m)] << " ";
}
std::cout << std::endl;
}
}
int main(int argc, char **argv) {
int fileArg = 0;
int FloatTypeSize = sizeof(float);
for (int i = 1; i < argc; i++) {
if (strcmp("-d", argv[i]) == 0) {
FloatTypeSize = sizeof(double);
} else {
fileArg = i;
}
}
if (fileArg == 0) {
std::cerr << "Usage: " << argv[0] << " [-d] matrixFile (litte endian)"
<< std::endl;
return 1;
}
std::ifstream binFile(argv[fileArg], std::ios::binary);
if (!binFile) {
std::cerr << "Cannot open " << argv[fileArg] << " for reading" << std::endl;
return 1;
}
unsigned int m, n;
binFile.read((char *)&m, sizeof(unsigned int));
binFile.read((char *)&n, sizeof(unsigned int));
// Raw array
char *A = new char[m * n * FloatTypeSize];
binFile.read((char *)A, m * n * FloatTypeSize);
binFile.close();
if (FloatTypeSize == sizeof(float)) {
printMatrix<float>((float *)A, m, n);
} else {
printMatrix<double>((double *)A, m, n);
}
delete[] A;
}