C++中簡單的文本文件輸入/輸出示例詳解
為了便于理解,我們把cout 用于控制臺輸出時的一些情況和寫入到文本文件的情況進行類比:
cout 控制臺輸出
包含頭文件 iostream
頭文件 iostream 定義了一個 ostream 類用于處理輸出
頭文件 iostream 聲明了一個名為 cout 的 ostream 對象
指明名稱空間 std
可以結合使用cout和運算符<<來顯示各種類型的數(shù)據(jù)
文本文件輸出(寫入到文本文件)
包含文件頭 fstream
頭文件 fstream 定義了一個ofstream 類用于處理輸出
聲明一個或多個 ofstream 對象,可以自由命名
指明名稱空間 std
把 3 中聲明的 ofstream 對象與文件關聯(lián)起來,比如使用 open()方法
使用完文件后,需要使用 close()方法將其關閉
還可以結合 ofstream 對象和運算符<<來輸出各種類型的數(shù)據(jù)
注意:cout 控制臺輸入輸出中,頭文件 iostream 聲明了一個名為 cout 的 ostream 對象,無需自己手動聲明;文件輸出中,我們必須自己動手聲明一個 ofstream 對象,為其命名,將其同文件關聯(lián)起來。請看下面的例子:
#include<fstream>
ofstream OutFile; //聲明一個 ofstream 對象
OutFile.open("study.txt"); //將OF與“study.txt”文件關聯(lián)起來
下面請看一個完整的程序,用戶輸入信息,將信息顯示到屏幕上,再將這些信息寫入到文本文件中:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
char name[20];
double height;
double weight;
ofstream outFile;//創(chuàng)建了一個ofstream 對象
outFile.open("information.txt");//outFile 與一個文本文件關聯(lián)
cout<<"Enter name: ";
cin.getline(automobile, 20);
cout<<"Enter height: ";
cin>>year;
cout<<"Enter weight: ";
cin>>weight;
// cout 控制臺輸出前面輸入的信息
cout<<fixed;
cout.precision(2);
cout.setf(ios_base::showpoint);
cout<<"Make and Model: "<<automobile<<endl;
cout<<"Year: "<<year<<endl;
cout<<"Was asking $"<<a_price<<endl;
cout<<"Now asking $"<<d_price<<endl;
// outFile 把信息寫入到文本文件
outFile<<fixed; //小數(shù)點格式顯示double
outFile.precision(2); //設置精度
outFile.setf(ios_base::showpoint); //強制顯示小數(shù)點后的零
outFile<<"Name: "<<name<<endl;
outFile<<"Height: "<<height<<endl;
outFile<<"Weight: "<<weight<<endl;
outFile.close(); //使用完文本文件后要用close()方法將其關閉
return 0;
}
接下來,我們再把cin 用于控制臺輸入時的一些情況和讀取文本文件的情況進行類比:
cin 控制臺輸出
包含頭文件 iostream
頭文件 iostream 定義了一個 istream 類用于處理輸出
頭文件 iostream 聲明了一個名為 cin 的 istream 對象
指明名稱空間 std
可以結合使用cin和運算符>>來輸入各種類型的數(shù)據(jù)
文本文件輸入(讀取文本文件)
包含文件頭 fstream
頭文件 fstream 定義了一個ifstream 類用于處理輸出
聲明一個或多個 ifstream 對象,可以自由命名
指明名稱空間 std
把 3 中聲明的 ifstream 對象與文件關聯(lián)起來,比如使用 open()方法
使用完文件后,需要使用 close()方法將其關閉
還可以結合 ifstream 對象和運算符>>來讀取各種類型的數(shù)據(jù)
可以用 cin 和 get() 方法來讀取一個字符,或用 cin 和 getline() 方法來讀取一行字符
可以結合使用 ifstream 和 eof()、fail()方法來判斷輸入是否成功
如果試圖打開一個不存在的文件用于輸入將會導致后面使用 ifstream 對象讀取數(shù)據(jù)時發(fā)生錯誤,因此用戶需要使用 is_open() 方法檢查文件是否被成功打開,如下:
#include <fstream>
#include <cstdlib>
ifstream InFile;
InFile.open("information.txt");
if(!Infile.is_open()){
exit(EXIT_FAILURE);
}
如果文件被成功打開, is_open() 方法將返回 true , exit()的原型在頭文件 cstdlib 中被定義,exit(EXIT_FAILURE) 用于終止程序。
下面請看一個完整的程序,用戶打開指定的文本文件,讀取文件中的 double 類型數(shù)據(jù),并在控制臺輸出所讀取數(shù)據(jù)的數(shù)目、總和以及平均數(shù):
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
const int SIZE = 60;
int main()
{
char fileName[SIZE];
ifstream InFile;
cout<<"Enter the name of data file: ";
cin.getline(fileName, SIZE);
cout<<fileName<<endl;
InFile.open(fileName);
if(!InFile.is_open()){
cout<<"Could not open the file "<< fileName <<endl;
cout<<"Program terminating.\n";
exit(EXIT_FAILURE);
}
double value;
double sum = 0.0;
int count = 0;
InFile >> value;
while(InFile.good()){
++count;
sum += value;
InFile >> value;
}
if (InFile.eof())
cout<<"End of file reached:\n";
else if (InFile.fail())
cout<<"Input terminated by data mismatch.\n";
else
cout<<"Input terminated for unknown reason.\n";
if(count == 0)
cout<<"No data processed.\n";
else{
cout<<"Items read: "<<count<<endl;
cout<<"Sum: "<<sum<<endl;
cout<<"Average: "<<sum/count<<endl;
}
InFile.close();
return 0;
}
總結
到此這篇關于C++中簡單的文本文件輸入/輸出的文章就介紹到這了,更多相關C++文本文件輸入/輸出內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Cocos2d-x學習筆記之CCLayerColor層的使用實例
這篇文章主要介紹了Cocos2d-x學習筆記之CCLayerColor層的使用實例,CCLayerColor是一個顏色布景層類,本文依然使用Hello World作為例子講解,需要的朋友可以參考下2014-09-09
詳解C++中StringBuilder類的實現(xiàn)及其性能優(yōu)化
在Java和C#中,StringBuilder可以創(chuàng)造可變字符序列來動態(tài)地擴充字符串,那么在C++中我們同樣也可以實現(xiàn)一個StringBuilder并且用來提升性能,下面就來詳解C++中StringBuilder類的實現(xiàn)及其性能優(yōu)化2016-05-05
C語言中不定參數(shù)?...?的語法以及函數(shù)封裝
不定參數(shù)是指函數(shù)可以接收不確定個數(shù)的參數(shù),下面這篇文章主要給大家介紹了關于C語言中不定參數(shù)?...?的語法以及函數(shù)封裝的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2023-01-01

