1、頭文件介紹
#include <iostream>//標準輸入輸出流#include <fstream>//派生自iostream,包括ifstream和ofstream
using namespace std;//都在名稱空間std中,別忘了加上
2、打開文件
const char* fileName="1.txt";//要打開的文件名,可以使路徑名,默認情況下是所建工程下
fstream類派生了兩個類ifstream/ofstream
fstream f(fileName,參數二);
參數二有多種形式,這里只看主要用的幾種:
ios_base::in//打開文件用于讀
ios_base::out//打開文件用于寫
ios_base::app//打開文件,用于追加(不覆蓋原有文件,默認情況是覆蓋)
ios_base::binary//以二進制方式打開文件,默認情況下是文本文件方式
例:
fstream i(fileName,ios_base::in|ios_base::out);//打開文件用于讀寫(既可讀也可寫)
ifstream in(fileName,ios_base::binary|ios_base::in);//以二進制方式打開文件用于讀
ofstream out(fileName,ios_base::out|ios_base::app);//打開文件用于追加
3、由于派生自iostream,很多其他的方法和iostream一樣
比如:seekg()/eof()/clear()……
4、一些文件操作的例子
讀寫二進制文件
[cpp] view plain copy#include "stdafx.h" #include <iostream> #include <fstream> #include <string> using namespace std; struct plant { char name[20]; int age; char num[10]; }; int _tmain(int argc, _TCHAR* argv[]) { plant p; p.name[20]=(char)"yao"; p.age=21; p.num[10]=(char)"0950420011"; ofstream pf("data.dat",ios_base::out|ios_base::app|ios_base::binary); pf.write((char*)&p,sizeof(p)); pf.close(); ifstream in("data.dat",ios_base::in|ios_base::binary); plant p1; in.read((char*)&p1,sizeof(p1)); in.close(); cout<<p1.age<<endl<<p1.name<<endl<<p1.num<<endl; return 0; }循環讀取指定文件的指定行
[cpp] view plain copy#include "stdafx.h" #include <iostream> #include <fstream> #include <string> #include <sstream> #include <time.h> //#include "ioman[cpp] view plain copy char c; do { fstream f; cout<<"請輸入文件名稱(注:先把你要打開的文件復制到本程序下,如:'file.txt')"<<endl; int flag=0; [cpp] view plain copy do { string str; if(flag) { f.clear(); cout<<"輸入文件不存在,請重新輸入:"<<endl; } cin>>str; const char* file=str.c_str(); //string text=""; f.open(file,ios_base::in|ios_base::out|ios_base::binary); flag++; } while(!f.is_open()); int lineCount=1;//記錄當前文件的總行數 int SumByte=0;//記錄文件的總大小 while(!f.eof()) { char c; c=f.get(); if(c=='/n') lineCount++; SumByte++; } f.clear(); float kb; kb=(float)SumByte/1024; cout<<"當前文件的字節數是:"<<SumByte<<" Bit"<<endl; cout<<"當前文件的總大小是:"<<kb<<" KB"<<endl; cout<<"當前文件的總行數是: "<<lineCount<<endl; cout<<"/*===================================================*//n/*===================================================*/"<<endl; int line; string str_line; cout<<"/n接下來請輸入你要讀取文件的行數:/n"; cin>>str_line; while(!IsDigital(str_line)) { cout<<"輸入格式不正確,請重新輸入:/n"; cin>>str_line; } line=atoi(str_line.c_str()); cout<<"當前的line值 :"<<line<<endl; while((line<=0)||(line>lineCount)) { cout<<"輸入數據有誤(1--"<<lineCount<<"間),請重新輸入:/n"; cin>>line; } cout<<"你選擇了輸出第"<<line<<"行"<<endl; //line=cin.get(); //string strOut;//輸出內容 cout<<"/*===================================================*//n/*===================================================*/"<<endl; int count=0; char ch; f.seekg(0);//指向文件開始位置 for(int i=1;i<line;i++) { /*do { //f.read((char*)&ch,sizeof ch); ch=f.get(); count++; } while(ch!='/n');*/ while(f.read((char*)&ch,sizeof ch)) { count++; if(ch=='/n') break; } } cout<<"輸出count的值:"<<endl; cout<<count<<endl; f.seekg(count);//跳轉到所選行數的開頭 cout<<"/*===================================================*//n"<<endl; cout<<"/n接下來輸出你所選擇的行:/n"; cout<<"/*===================================================*//n"<<endl; char a; while(f.read((char*)&a,sizeof a)) { cout<<a; if(a=='/n') break; } cout<<"////////////////////////////////////////////////////////////////"<<endl; cout<<"是否繼續打開文件?是-選擇y:否-按任意鍵結束(除y)"<<endl; cin>>c; } while(c=='y'); // char p; //cin>>p; return 0; }
復制文件
[cpp] view plain copy#include "stdafx.h" #include <iostream> #include <fstream> using namespace std; const char* file1="1.txt"; const char* file2="2.txt"; //把文件1的內容寫到文件2的末尾 int _tmain(int argc, _TCHAR* argv[]) { ifstream in(file1);//默認方式打開文件1進行讀 ofstream out(file2,ios_base::out|ios_base::app);//打開文件2進行寫入(app為追加) out<<in.rdbuf();//將文件1的內容輸入到文件2的流中 in.close();//關閉文件流 out.close(); return 0; }C++文件操作就介紹到這里
頂新聞熱點
疑難解答
圖片精選