8. 檔案讀寫操作
1 #include<iostream> 2 #include<string> 3 #include<fstream> // 讀寫檔案 頭檔案 4 using namespace std; 5 6 // 檔案操作 7 8 // 寫檔案 9 void writefile() { 10 11 ofstream ofs; 12 13 ofs.open("test.txt", ios::out); 14 15 ofs << "姓名:張三" << endl; 16 ofs << "性別:男" << endl; 17 ofs << "年齡:18" << endl; 18 19 ofs.close(); 20 21 } 22 23 // 讀檔案 24 void readfile() { 25 26 ifstream ifs; 27 28 // 打開檔案,判斷是否打開成功 29 ifs.open("test.txt", ios::in); 30 if (!ifs.is_open()) { 31 cout << "檔案打開失敗" << endl; 32 return; 33 } 34 35 // 讀資料,4種方式 36 // 第一種 37 cout << "第一種讀資料方式" << endl; 38 char buf1[1024] = { 0 }; 39 while ( ifs >> buf1 ) 40 { 41 cout << buf1 << endl; 42 } 43 44 // 第二種 45 cout << "第二種讀資料方式" << endl; 46 char buf2[1024] = { 0 }; 47 while (ifs.getline(buf2, sizeof(buf2))) 48 { 49 cout << buf2 << endl; 50 } 51 52 // 第三種 53 string buf3; 54 cout << "第三種讀資料方式" << endl; 55 while (getline(ifs, buf3)) 56 { 57 cout << buf3 << endl; 58 } 59 60 // 第四種 61 cout << "第四種讀資料方式" << endl; 62 char c; 63 while ((c = ifs.get()) != EOF) // EOF: end of file 64 { 65 cout << c; 66 } 67 68 ifs.close(); 69 70 } 71 72 73 class Person { 74 public: 75 char m_Name[64]; 76 int m_Age; 77 }; 78 // 二進制檔案-寫操作 79 void writefile_binary() { 80 81 ofstream ofs("person.txt", ios::out | ios::binary); 82 83 //ofs.open("person.txt", ios::out | ios::binary) 84 85 Person p = { "張三", 18 }; 86 ofs.write((const char*)&p, sizeof(Person)); 87 88 ofs.close(); 89 } 90 91 // 二進制檔案-讀操作 92 void readfile_binary() { 93 94 ifstream ifs; 95 96 ifs.open("person.txt", ios::in | ios::binary); 97 if (!ifs.is_open()) { 98 cout << "檔案打開失敗" << endl; 99 return; 100 } 101 102 Person p; 103 ifs.read((char*)&p, sizeof(Person)); 104 cout << "姓名:" << p.m_Name << " 年齡:" << p.m_Age << endl; 105 106 ifs.close(); 107 } 108 109 110 111 int main() { 112 113 //writefile(); 114 115 //readfile(); 116 117 //writefile_binary(); 118 119 readfile_binary(); 120 121 122 system("pause"); 123 124 return 0; 125 } 126 127 // 總結 128 // 對檔案的操作,首先包含頭檔案<fstream> 129 // 130 // 文本檔案:檔案以ASCII形式存在在計算機中 131 // 二進制檔案:以文本的二進制形式存盤于計算機中 132 // 133 // 操作檔案三類: 134 // ofstream: 寫操作 135 // ifstream: 讀操作 136 // fstream: 讀寫操作 137 // 138 // 讀檔案一般步驟: 139 // 1. 包含頭檔案 #include <fstream> 140 // 2. 創建流物件 ofstream ofs; 141 // 3. 打開檔案 ofs.open("檔案路徑",打開方式) 142 // 4. 寫資料 ofs << "寫入資料"; 143 // 5. 關閉檔案 ofs.close(); 144 // 145 // 寫檔案一般步驟: 146 // 1. 包含頭檔案 #include <fstream> 147 // 2. 創建流物件 ofstream ifs; 148 // 3. 打開檔案 ifs.open("檔案路徑",打開方式) 149 // 4. 讀資料:四種方式讀取 150 // 5. 關閉檔案 ifs.close(); 151 // 152 // 153 // 打開方式: 154 // ios::in 為讀檔案而打開檔案 155 // ios::out 為寫檔案而打開檔案 156 // ios::ate 初始位置:檔案尾 157 // ios::app 追加方式寫檔案 158 // ios::trunc 如果檔案存在,先洗掉,再創建 159 // ios::binary 二進制方式 160 // 注意:檔案打開方式可以配合使用,利用 | 運算子 161 // 如:用二進制方式寫檔案 ios::binary | ios::out 162 // 163 // 164 // 對于二進制檔案的讀寫操作 165 // 打開方式要指定為 ios::binary 166 // 二進制方式寫檔案主要利用物件呼叫成員函式write 167 // 函式原型: ostream& write(const char* buffer, int len); 168 // 引數解釋:字符指標buffer指向記憶體中一段存盤空間,len是讀寫的位元組數 169 // 170 // 二進制方式讀檔案主要利用物件呼叫成員函式read 171 // 函式原型:istream& read (char* buffer, int len); 172 // 引數解釋:字符指標buffer指向記憶體中一段存盤空間,len是讀寫的位元組數 173 //
參考《黑馬程式員》C++教程
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/469570.html
標籤:C++
上一篇:C++基礎-檔案操作
下一篇:epoll 函式決議
