稱爲文件,並用一個名字(稱爲文件名)加以標識ios
編碼方式: 文本文件 二進制文件app
存取方式: 順序文件 隨機文件函數
ifstream、ofstream 和 fstream 類用於內存與文件之間的數據傳輸編碼
1.文件操做的基本步驟:spa
(1).打開文件指針
(2).讀/寫文件code
(3).關閉文件對象
(1).打開文件:blog
包括創建文件流對象;與外部文件關聯;指定文件的打開方式ip
打開文件有兩種方法:
流類 對象名 ;
對象名 . open ( 文件名 , 方式 ) ;
流類 對象名 ( 文件名 , 方式 ) ;
流類能夠使用 ifstream、ofstream、fstream;
open函數的原型:void open (const char* ,int mode,int =filebuf::openprot);
1.其中,第一個參數表示相關聯的文件名
2.第二個參數表示文件的打開方式
標識常量 |
值 |
意義 |
ios::in |
0x0001 |
讀方式打開文件 |
ios::out |
0x0002 |
寫方式打開文件 |
ios::ate |
0x0004 |
打開文件時,指針指向文件尾 |
ios::app |
0x0008 |
追加方式 |
ios::trunc |
0x0010 |
刪除文件現有內容 |
ios::nocreate |
0x0020 |
若是文件不存在,則打開操做失敗 |
ios::noreplace |
0x0040 |
若是文件存在,則打開操做失敗 |
ios::binary |
0x0080 |
二進制方式打開,默認爲文本方式 |
3.第三個參數是文件的保護方式,通常只用缺省值
filebuf::openprot 適應MS-DOS 模式
filebuf::sh_compat 適應MS-DOS 模式
filebuf::sh_none 無模式
filebuf::sh_read 讀模式
filebuf::sh_write 寫模式
filebuf、ifstream、ofstream、fstream的構造函數具備相同的參數和缺省值
文件流的構造函數和 open ( ) 函數用於打開文件,析構函數在流對象被刪除以前關閉文件
用第一種方式打開文件:
ifstream infile ; // 創建輸入文件流對象
infile.open( "datafile.dat" , ios::in ) ; // 鏈接文件,指定打開方式
ofstream outfile ; // 創建輸出文件流對象
outfile.open( "d:\\newfile.dat" , ios::out ) ; // 鏈接文件,指定打開方式
第二種方法 調用文件流類帶參數構造函數 :
ifstream infile ( "datafile.dat" , ios::in ) ;
ofstream outfile ( "d:\\newfile.dat" , ios::out );
fstream rwfile ( "myfile.dat" , ios::in | ios::out ) ; 用或運算符 「|」 鏈接兩個表示打開方式的標識常量
2.關閉文件
切斷流對象和外部文件的鏈接
1 例如: 2 ofstream ofile ; // 建立輸出文件流 3 ofile . open ( "myfile1" ) ; // ofile流與文件「myfile1」相關聯,等價於Ofstream ofile(「myfile1」); 4 …… // 訪問文件「myfile1」 5 ofile . close ( ) ;// close函數關閉文件「myfile1」,但流對象任然存在 6 ofile . open ( "myfile2" ); // 重用ofile流 7
2.open函數和close函數的應用:
1 #include <iostream> 2 #include <fstream> 3 using namespace std; 4 5 void main() 6 { 7 ofstream ost; //建立輸出流對象 8 ost.open("d:\\xiaoxue");//創建文件關聯,缺省爲文件模式 9 ost << 20 << endl; //向流插入數據 10 ost << 3.5 << endl; 11 ost.close(); //關閉文件 12 ifstream ist("d:\\xiaoxue"); ;//建立輸入流並創建文件關聯, 13 int n; 14 double d; 15 ist >> n >> d; //從流提取數據 16 cout << n << '\t' << d << endl; //向預約義流插入數據 17 }
3.文本文件:
換行符、製表符等分隔
1 #include <iostream> 2 #include <fstream> 3 #include <iomanip> 4 using namespace std; 5 6 void main() 7 { 8 ofstream ost("d:\\my.txt");//默認方式打開文本文件 9 ost << "1234567890" << endl; //插入字符串 10 int a = 123;//把整形數轉換成字符串 11 ost << a << endl; 12 13 //以指定格式插入數據 14 ost << setw(10) << a << endl; 15 //關閉右對齊,設置左對齊,設置填充符號,設置域寬, 16 ost << resetiosflags(ios::right) << setiosflags(ios::left) << setfill('*') << setw(10) << a << endl; 17 //關閉左對齊,設置右對齊,設置精度,設置域寬 18 ost << resetiosflags(ios::left) << setiosflags(ios::right) << setprecision(6) << setw(10) << 12.34567890 << endl; 19 //關閉文件 20 ost.close(); 21 }
複製文件:
1 #include <fstream> 2 #include <iostream> 3 using namespace std; 4 //複製文件 5 int main() 6 { 7 ifstream ist("d:\\test.txt"); 8 if(!ist) 9 { 10 cout << "Connot open 'text' of input"<<endl; 11 return 0; 12 } 13 ofstream ost("d:\\test1.txt"); 14 if(!ost) 15 { 16 cout << "Connot open file!" << endl; 17 return 0; 18 } 19 char ch; 20 while(ist && ist.get(ch)) 21 { 22 ost.put(ch); 23 } 24 ost.close(); 25 ist.close(); 26 return 1; 27 }
在文件結尾插入字符串:
1 #include <fstream> 2 using namespace std; 3 void main() 4 { 5 ofstream ost("d:\\test1.txt",ios::app);//追加方式 6 char s[20] ="\tStraightforward!"; 7 ost << s; //插入字符串 8 ost.close(); 9 }
創建一個包含學生成績、學號、和姓名的文件,並輸出,最高分、最低分、平均分等其餘存入的信息
1 #include <iostream> 2 #include <fstream> 3 using namespace std; 4 5 void main() 6 { 7 char fileName[30],name[30],num[30],title[50]; 8 int n=0; 9 double max,min,ave,total=0,score; 10 cout << "Please input the name of students file!"<< endl; 11 cin >> fileName; 12 ofstream ost(fileName,ios::out); 13 if(!ost) 14 { 15 cout<< "File could not be open!"<<endl; 16 } 17 ifstream ist(fileName,ios::in); 18 if(!ist) 19 { 20 cout << "File could not be open!" << endl; 21 } 22 ost << "\t\t\t\t\t\t學生成績文件\n"; 23 cout << "Input num、name and score:(Enter Ctrl-Z to end input.)\n"; 24 while(cin >> num >> name >> score) 25 { 26 ost << num <<'\t' << name << '\t' << score << '\n'; 27 } 28 ost.close(); 29 ist.getline(title,40);//略去標題行 30 while(ist >> num >> name >> score) //提取數據 31 { 32 cout << num << '\t' << name << '\t' << score << endl; 33 if(n==0) 34 { 35 min = max = score; 36 }else 37 { 38 if(score>max) 39 { 40 max = score; 41 } 42 if(score<min) 43 { 44 min = score; 45 } 46 } 47 n++; 48 total +=score; //統計 49 } 50 ave = total/n; 51 cout<< "maximal is" << max << "\n" << "minimal is" << min << '\n' << "average is" << ave << endl; 52 ist.close(); 53 }
瀏覽文件
1 #include <iostream> 2 #include <fstream> 3 using namespace std; 4 void main() 5 { 6 void browseFile(char*,int); 7 char fileName[40]; 8 cout << "Please input name of file!"<<endl; 9 cin>>fileName; 10 browseFile(fileName,1); 11 } 12 void browseFile(char *fileName,int delLine) 13 { 14 ifstream ist(fileName,ios::in); 15 char s[80]; 16 for(int i=1;i<=delLine;i++) 17 { 18 ist.getline(s,80); 19 } //不顯示開始指定的行數 20 while(!ist.eof()) 21 { 22 ist.getline(s,80);//按行讀取文件 23 cout << s << endl;//按行顯示文件 24 } 25 ist.close(); 26 }