实验六 模板类和文件I/O
task3_1.cpp
1 #include <iostream> 2 #include <fstream> 3 #include <array> 4 #define N 5 5 6 int main() { 7 using namespace std; 8 9 array<int, N> x{ 97,98,99,100,101 }; 10 11 ofstream out; 12 out.open("data1.dat", ios::binary); 13 if (!out.is_open()) { 14 cout << "fail to open data1.dat\n"; 15 return 1; 16 } 17 18 // 把从地址&x开始连续sizeof(x)个字节的数据块以字节数据方式写入文件data1.txt 19 out.write(reinterpret_cast<char*>(&x), sizeof(x)); 20 out.close(); 21 }
task3_2.cpp
1 #include <iostream> 2 #include <fstream> 3 #include <array> 4 #define N 5 5 6 int main() { 7 using namespace std; 8 array<int, N> x; 9 10 ifstream in; 11 in.open("data1.dat", ios::binary); 12 if (!in.is_open()) { 13 cout << "fail to open data1.dat\n"; 14 return 1; 15 } 16 17 // 从文件流对象in关联的文件data1.dat中读取sizeof(x)字节数据写入&x开始的地址单元 18 in.read(reinterpret_cast<char *>(&x), sizeof(x)); 19 in.close(); 20 21 for (auto i = 0; i < N; ++i) 22 cout << x[i] << ", "; 23 cout << "\b\b \n"; 24 }
对task3_2.cpp的代码稍做改动,把line8的数组类型从int类型修改成char类型,即: array<int, N> x; ----> 修改成: array<char, N> x;
其它不做任何改动,再次运行程序,观察运行结果是什么?尝试分析原因:为什么会是那样的结果?
1 #include <iostream> 2 #include <fstream> 3 #include <array> 4 #define N 5 5 6 int main() { 7 using namespace std; 8 array<char, N> x; 9 10 ifstream in; 11 in.open("data1.dat", ios::binary); 12 if (!in.is_open()) { 13 cout << "fail to open data1.dat\n"; 14 return 1; 15 } 16 17 // 从文件流对象in关联的文件data1.dat中读取sizeof(x)字节数据写入&x开始的地址单元 18 in.read(reinterpret_cast<char *>(&x), sizeof(x)); 19 in.close(); 20 21 for (auto i = 0; i < N; ++i) 22 cout << x[i] << ", "; 23 cout << "\b\b \n"; 24 }
发生上述现象是因为char和int类型所占用的字节数不同,从而导致在修改后从文件读出数据时是以char型读取,读取时是一个一个字节读取,从而会产生空格。
Vector.hpp
1 #pragma once 2 using namespace std; 3 4 template<typename T> 5 class Vector { 6 public: 7 Vector(int n0) :n{ n0 } { x = new T[n]; } 8 Vector(int n0, T t) :n{ n0 } { 9 x = new T[n]; 10 for (int i = 0; i < n; i++) 11 x[i] = t; 12 } 13 Vector(const Vector<T>& v) :n{ v.n } { 14 x = new T[n]; 15 for (int i = 0; i < n; i++) 16 x[i] = v.x[i]; 17 } 18 ~Vector() = default; 19 20 int get_size() const { return size; } 21 T& at(int i) const { return x[i]; } 22 T& operator[](const int i) { return x[i]; } 23 24 friend void output(Vector& v) { 25 cout << v.x[0]; 26 for (int i = 1; i < v.n; i++) 27 cout << ", " << v.x[i]; 28 cout << endl; 29 } 30 31 private: 32 int n; 33 T* x; 34 };
task4.cpp
1 #include <iostream> 2 #include "Vector.hpp" 3 4 void test() { 5 using namespace std; 6 7 int n; 8 cin >> n; 9 10 Vector<double> x1(n); 11 for (auto i = 0; i < n; ++i) 12 x1.at(i) = i * 0.7; 13 14 output(x1); 15 16 Vector<int> x2(n, 42); 17 Vector<int> x3(x2); 18 19 output(x2); 20 output(x3); 21 22 x2.at(0) = 77; 23 output(x2); 24 25 x3[0] = 999; 26 output(x3); 27 } 28 29 int main() { 30 test(); 31 }
task5.cpp
1 #include <iostream> 2 #include <fstream> 3 #include <iomanip> 4 5 using namespace std; 6 7 void output(ostream& out) { 8 out << " "; 9 int a[200]; 10 for (int i = 97; i < 123; i++) 11 a[i] = i; 12 for (int i = 97; i < 123; i++) 13 out << char(a[i]) << " "; 14 out << endl; 15 for (int i = 1; i <= 26; i++) { 16 out << setw(2) << right << i; 17 for (int j = 97; j < 123; j++) { 18 int t = a[j] + i - 32; 19 if (a[j] + i > 122) 20 t -= 26; 21 out << setw(2) << right << char(t); 22 } 23 out << endl; 24 } 25 } 26 27 int main() { 28 output(cout); 29 ofstream out; 30 out.open("cipher_key.txt"); 31 if (!out.is_open()) { 32 cout << "fail to open cipher_key.txt!\n"; 33 return 0; 34 } 35 output(out); 36 out.close(); 37 }