1.8 简单的文件输入输出
1. 將數據寫入文本文件中, 包含幾個要點:
?? (1) 必須包含頭文件fstream
?? (2) 頭文件fstream中定義了一個用于處理輸出的ofstream類
?? (3) 需要聲明一個或多個ofstream對象
?? (4) 需要將ofstream對象與文件關聯起來, 方法之一是使用open()
?? (5) 使用完文件后, 應使用close()方法將其關閉
?? (6) 可結合使用ofstream對象和操作符<<來輸出各種類型的數據.
?? 定義一個ofstream類, ofstream outfile1; 然后將其與文件關聯起來,使用對象的open方法來關聯文件.例如下面幾種:
?? (1) 直接以文件路徑作為open參數:outfile1.open("fish.txt"). 如果沒有fish.txt文件, open()方法將會創建一個fish.txt文件, 如果已經包含了一個fish.txt文件, open()將首先丟棄其原先的內容, 然后將新的輸入加入到文件中.(當然有方法讓我們保留原來的內容)
?? (2) open()以字符串作為參數:
???????? char filename[50];
???????? cin>>filename;
???????? outfile1.open(filename);
???創建了ofstream對象, 并與文件關聯之后, 下面是怎么使用此對象了. 使用方法與cout對象類似, 結合<<操作符. 例如:
???????? double w=125.8;
???????? outfile1<<w; //將w寫入對象outfile1所關聯的文件fish.txt中
?????????char line[81]="I have a dream!";
???????? outfile1<<line<<endl; //將line中內容和一個換行符輸入對象outfile1所關聯的文件fish.txt中
???總結文件輸出的主要步驟:
???(1) 包含頭文件fstream; #include<fstream>
?? (2) 創建一個ofstream對象; ofstream outfile1;
?? (3) 將ofstream對象同一個文件關聯起來; outfile1.open("fish.txt");
?? (4) 想使用cout那樣使用ofstream對象; outfile1<<line<<endl;
?#include<iostream>
#include<fstream>
using namespace std;int main()
{char automobile[50];int year;double a_price;ofstream outfile;outfile.open("fish.txt");cout<<"Enter your automobile's information: ";cin>>automobile;outfile<<"Enter your automobile's information: "<<automobile<<endl;cout<<"When your buy it: ";cin>>year;outfile<<"When your buy it: "<<year<<endl;cout<<"What's the price: ";cin>>a_price;outfile<<"What's the price: "<<a_price<<endl;outfile.close();/分割線,下面是數據//18 19 18.5 13.5 14//16 19.5 20 18 12 18.5char filename[60];ifstream infile;cin.get();//吸收一個換行符cout<<endl<<"Enter name of data file: ";cin.getline(filename,60);infile.open(filename);if(!infile.is_open()){cout<<"Fail to open the file!"<<endl;}double value;double sum=0;int count=0;infile>>value; //讀取第一個一個double數據賦值給valuewhile(infile.good())//如果輸入正確并且沒有到文件結尾處(EOF),循環結束則可能有多個原因:1.到達文件尾部EOF,2.輸入不正確,數據類型不匹配,3其他原因{++count;sum+=value;infile>>value;//繼續讀取數據}if(infile.eof())//如果到達文件尾部而停止while,則cout<<"Reach the end of file."<<endl;else if(infile.fail())//如果是因為數據不匹配,則cout<<"Input terminated by data mismatch./n";else//可能因為未知原因cout<<"Input terminated for unknown reason./n";infile.close();ofstream outfile2;outfile2.open("output.txt");if(count==0)//沒有數據被處理cout<<"No data processed./n";else//有數據被處理則輸出處理結果{cout<<"Items read: "<<count<<endl;cout<<"Sum: "<<sum<<endl;cout<<"Average: "<<sum/count<<endl;outfile2<<"Items read: "<<count<<endl;//輸出結果到文件output.txtoutfile2<<"Sum: "<<sum<<endl;outfile2<<"Average: "<<sum/count<<endl;}outfile2.close();//輸出://Items read: 10//Sum: 168.5//Average: 16.85system("pause");return 0;
}??
2. 讀入文本數據, 包含幾個要點
?? (1) 必須包含頭文件fstream
?? (2) 頭文件fstream定義了一個用于處理輸入的ifstream類
?? (3) 需要聲明一個ifstream對象
?? (4) 將ifstream對象與文件關聯
?? (5) 結合操作符>>來讀取各種類型數據
?? (6) 使用ifstream對象的get()方法來讀取一個字符, getline()方法讀取一行字符
?? (7) 如果最后一個讀取操作成功, 返回true, 否則返回false.
?? 定義個ifstream對象: ifstream infile; 然后將這個對象與文件關聯起來, 使用open()方法來關聯文件. infile.open("fish.txt"); 這樣使用對象:
?? double wt;
?? infile>>wt; //從file中讀取一個數據
?? char line[81];
?? infile.getline(line, 81);//讀取一行字符存入line中
?? 如果打開的文件不存在, 則無法對對象進行輸入, 用is_open()方法判斷,?成功打開文件, 則infile.is_open()返回true, 否則返回false.
?? if(!infile.is_open())
???????cout<<"fail to open the file/n";
???示例看上面的程序
?
總結
以上是生活随笔為你收集整理的1.8 简单的文件输入输出的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 1.7 时间延时器和类的别名
- 下一篇: 1.9 函数-C++编程模块