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
| #include <iostream> #include <fstream> // ofstream类需要包含的头文件。 using namespace std; int main() { string filename = R"(D:\data\bin\test.dat)"; ofstream fout; fout.open(filename, ios::app | ios::binary); if (fout.is_open() == false) { cout << "打开文件" << filename << "失败。\n"; return 0; } struct Data { char name[31]; int no; }data; data = { "zq",3}; fout.write((const char*)&data, sizeof(Data)); data = { "qq",8}; fout.write((const char*)&data, sizeof(Data)); fout.close(); cout << "操作文件完成。\n"; }
|