1 fstream
2 ifstream
3 ofstream
4 seekg
5 seekp
6 tellg
7 tellp
1 fstream
打開輸入輸出文件流
1 #include <iostream> 2 #include <fstream> 3 4 void main() 5 { 6 std::fstream fio("F:\\1.txt", std::ios::in | std::ios::out); 7 8 fio << "hello" << std::endl;//寫入 9 fio << "world" << std::endl; 10 fio << "hello" << std::endl; 11 fio << "china" << std::endl; 12 13 fio.seekg(std::ios::beg);//文件指針回到文件開頭,beg,begin 14 15 for (int i = 0; i < 4; i++)//讀取 16 { 17 char str[100] = { 0 }; 18 fio.getline(str, 100); 19 std::cout << str << std::endl; 20 } 21 22 fio.close();//關閉文件 23 24 system("pause"); 25 }
2 ifstream
打開輸入文件流
1 #include <iostream> 2 #include <fstream> 3 4 void main() 5 { 6 std::ifstream fin("F:\\1.txt");//創建讀取文件流 7 8 char str[100] = { 0 }; 9 10 fin >> str;//讀取 11 12 fin.close();//關閉文件 13 14 std::cout << str; 15 16 system("pause"); 17 }
3 ofstream
打開輸出文件流
打開文件,按行寫入
std::endl換行
1 #include <iostream> 2 #include <fstream> 3 4 void main() 5 { 6 std::ofstream fout; 7 8 fout.open("F:\\1.txt");//打開文件,如果沒有文件,將會創建新的文件 9 10 fout << "hello" << std::endl;//寫入,std::endl換行 11 fout << "china" << std::endl; 12 fout << "hello" << std::endl; 13 fout << "world" << std::endl; 14 15 fout.close();//關閉文件 16 17 system("pause"); 18 }
std::ios::app
打開文件以便在文件的尾部添加數據
1 #include <iostream> 2 #include <fstream> 3 4 void main() 5 { 6 std::ofstream fout("F:\\1.txt", std::ios::app);//打開輸出文件流 7 8 fout << "hello world hello china";//寫入 9 10 fout.close();//關閉文件 11 12 system("pause"); 13 }
復制文本
ifstream負責讀取,ofstream負責寫入
按照字節的方式讀寫二進制
文件加密解密都需要字節的方式
1 #include <iostream> 2 #include <fstream> 3 4 //按照字節的方式讀寫二進制 5 //文件加密解密都需要字節的方式 6 7 void main() 8 { 9 std::ifstream fin("F:\\1.txt", std::ios::binary);//需要復制的文件 10 std::ofstream fout("F:\\new.txt", std::ios::binary);//保存復制后的文件 11 12 if (!fin || !fout) 13 { 14 std::cout << "文件打開失敗" << std::endl; 15 return; 16 } 17 18 std::cout << "文件復制開始" << std::endl; 19 char ch = 0; 20 21 while (fout && fin.get(ch))//引用的方式讀取到一個字符 22 { 23 fout.put(ch);//寫入一個字節 24 } 25 26 fin.close();//關閉文件 27 fout.close();//關閉文件 28 29 std::cout << "文件復制完成" << std::endl; 30 31 system("pause"); 32 }
4 seekg
對輸入流操作:seekg()
fin.seekg(9, std::ios::beg);//從開始往后9個字符
fin.seekg(-9, std::ios::end);//從尾部倒數9個字符
1 #include <iostream> 2 #include <fstream> 3 4 void main()//二進制隨機位置讀取 5 { 6 double db[10] = { 1.1,2.2,3.3,4.4,5.5,6.6,7.7,8.8,9.9,10.1 }; 7 8 std::ofstream fout("F:\\1.txt", std::ios::binary);//寫入文件 9 fout.write((char *)db, 80);//寫入以字節為單位,因此轉換為char類型 10 fout.close(); 11 12 double *p = new double[5]; 13 14 std::ifstream fin("F:\\1.txt", std::ios::binary);//讀取文件 15 16 fin.seekg(-40, std::ios::end);//指針到達尾部前40個字節 17 18 fin.read((char *)p, 40); 19 fin.close(); 20 21 for (int i = 0; i < 5; i++) 22 { 23 std::cout << p[i] << std::endl; 24 } 25 26 system("pause"); 27 }
5 seekp
對輸出流操作:seekp()
seekp(9, std::ios::beg);//確定隨機位置進行讀寫
1 #include <iostream> 2 #include <fstream> 3 4 void main() 5 { 6 std::ofstream fout("F:\\1.txt");//寫入文件 7 fout << "1234567890abcdefghijklmn"; 8 fout.close();//關閉文件 9 10 std::ofstream Fout("F:\\1.txt", std::ios::in | std::ios::out);//寫入文件 11 char str[] = "helloworld"; 12 13 Fout.seekp(9, std::ios::beg);//確定隨機位置進行讀寫 14 15 Fout << str;//輸出到文件 16 Fout.close();//關閉文件 17 18 std::ifstream fin("F:\\1.txt");//讀取文件 19 char ch; 20 21 while (fin.get(ch))//打印 22 { 23 std::cout << ch; 24 } 25 fin.close();//關閉文件 26 27 system("pause"); 28 }
6 tellg
7 tellp
對輸出流操作:tellp()
long size = Fout.tellp();//當前位置距離begin有多少個字節,到尾部可以獲取文件大小
1 #include <iostream> 2 #include <fstream> 3 4 void main() 5 { 6 std::ofstream fout("F:\\1.txt");//寫入文件 7 fout << "1234567890abcdefghijklmn"; 8 fout.close();//關閉文件 9 10 std::ofstream Fout("F:\\1.txt", std::ios::in | std::ios::out);//寫入文件 11 char str[] = "helloworld"; 12 13 Fout.seekp(9, std::ios::end);//確定隨機位置進行讀寫 14 long size = Fout.tellp();//當前位置距離begin有多少個字節,到尾部可以獲取文件大小 15 16 Fout << str;//輸出到文件 17 Fout.close();//關閉文件 18 19 std::ifstream fin("F:\\1.txt");//讀取文件 20 char ch; 21 22 while (fin.get(ch))//打印 23 { 24 std::cout << ch; 25 } 26 fin.close();//關閉文件 27 28 system("pause"); 29 }