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
| #include <bits/stdc++.h> using namespace std;
void test01() { ofstream ofs; ofs.open( "D:\\coding_file\\study\\C++_file\\Other_file\\note_file\\Other\\File_" "Operation\\in.txt", ios::out); ofs << "hello world" << endl; ofs << "hello C++" << endl; ofs.close(); }
void test02() { ifstream ifs; ifs.open( "D:\\coding_file\\study\\C++_file\\Other_file\\note_file\\Other\\File_" "Operation\\in.txt", ios::in); if (!ifs.is_open()) { cout << "fail" << endl; return; } else { char buf[1024] = {0}; while (ifs >> buf) { cout << buf << endl; }
ifs.close(); } }
class Person { public: char name[64]; int age; }; void test03() { ofstream ofs( "D:\\coding_file\\study\\C++_file\\Other_file\\note_file\\Other\\File_" "Operation\\b_in.txt", ios::out | ios::binary); Person p = {"张三", 18}; ofs.write((const char*)&p, sizeof(p)); ofs.close(); }
void test04() { ifstream ifs( "D:\\coding_file\\study\\C++_file\\Other_file\\note_file\\Other\\File_" "Operation\\b_in.txt", ios::in | ios::binary); if (!ifs.is_open()) { cout << "fail" << endl; return; } else { Person p; ifs.read((char*)&p, sizeof(p)); cout << p.name << endl; cout << p.age << endl; } ifs.close(); } int main() { ios::sync_with_stdio(false); cout.tie(NULL); test01(); test02(); test03(); test04(); return 0; }
|