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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
| #include<iostream> #include<fstream> #include<string> #include<cstdlib>
using namespace std;
char HEX[16] = { '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' };
void transfrom(int num, char* hexNumber) { for (int i = 0; i < 8; i++) { hexNumber[i] = '0'; } int index = 7; while (num != 0 && index >= 0) { hexNumber[index--] = HEX[num % 16]; num = num / 16; } }
string getFileName(string& filename) { int index = -1; for (int i = filename.length() - 1; i >= 0; i--) { if (filename[i] == '\\') { index = i; break; } } return filename.substr(index + 1, filename.length()); }
int main() { int num = 0; string path_r; string path_w; cout << "Please input the File for read and write's name: " << endl;
ifstream in; ofstream out;
while (true) { cout << "The file path to read: "; getline(cin, path_r); in = ifstream(path_r, ios::binary); if (!in.is_open()) { cout << "Error: File Path is Wrong" << endl; } else break; } cout << "The File Path to save(.txt): "; getline(cin, path_w); out = ofstream(path_w);
long long Beg = in.tellg(); in.seekg(0, ios::end); long long End = in.tellg(); long long fileSize = End - Beg; in.seekg(0, ios::beg); out << "\t\t" << getFileName(path_r) << "\tFile Size: " << fileSize / 1024.0 << "KB" << endl << endl;
char hexNumber[9] = "00000000";
unsigned char temp; out << "\t\t"; for (int i = 0; i < 16; i++) out << HEX[i] << " "; out << endl;
while (in.read((char*)&temp, 1)) { if (num % 16 == 0) { out << endl; transfrom(num, hexNumber); out << hexNumber << ":\t"; } num++; int hex = (unsigned)temp; char a = HEX[hex / 16]; char b = HEX[hex % 16]; out << a << b << " "; }
out.seekp(0,ios::beg); in.close(); out.close();
cout << "Read Successfully" << endl; system("pause"); return 0; }
|