1 fstream
2 Ifstream
3 Ofstream
1 fstream
Open the input and output file stream
1#include <iostream>2#include <fstream>3 4 voidMain ()5 {6Std::fstream Fio ("F:\\1.txt", Std::ios::inch| Std::ios:: out);7 8Fio <<"Hello"<< Std::endl;//Write9Fio <<" World"<<Std::endl;TenFio <<"Hello"<<Std::endl; OneFio <<" China"<<Std::endl; A -Fio.close ();//Close File - the { -Std::fstream Fio ("F:\\1.txt", Std::ios::inch| Std::ios:: out);//reopen the file, the file pointer starts from the beginning - - for(inti =0; I <4; i++)//Read + { - Charstr[ -] = {0 }; +Fio.getline (str, -); AStd::cout << str <<Std::endl; at } - -Fio.close ();//Close File - } - -System"Pause"); in}
2 Ifstream
Open input File stream
1#include <iostream>2#include <fstream>3 4 voidMain ()5 {6Std::ifstream Fin ("F:\\1.txt");//Create read file stream7 8 Charstr[ -] = {0 };9 TenFin >> str;//Read One AFin.close ();//Close File - -Std::cout <<str; the -System"Pause"); -}
3 Ofstream
Open output File stream
Open file, write by line
Std::endl line break
1#include <iostream>2#include <fstream>3 4 voidMain ()5 {6 Std::ofstream Fout;7 8Fout.open ("F:\\1.txt");//Open the file, and if there is no file, a new file will be created9 TenFout <<"Hello"<< Std::endl;//Write, Std::endl line break OneFout <<" China"<<Std::endl; AFout <<"Hello"<<Std::endl; -Fout <<" World"<<Std::endl; - theFout.close ();//Close File - -System"Pause"); -}
#include <fstream>