網站首頁 編程語言 正文
為了便于理解,我們把cout 用于控制臺輸出時的一些情況和寫入到文本文件的情況進行類比:
cout 控制臺輸出
包含頭文件 iostream
頭文件 iostream 定義了一個 ostream 類用于處理輸出
頭文件 iostream 聲明了一個名為 cout 的 ostream 對象
指明名稱空間 std
可以結合使用cout和運算符<<來顯示各種類型的數據
文本文件輸出(寫入到文本文件)
包含文件頭 fstream
頭文件 fstream 定義了一個ofstream 類用于處理輸出
聲明一個或多個 ofstream 對象,可以自由命名
指明名稱空間 std
把 3 中聲明的 ofstream 對象與文件關聯起來,比如使用 open()方法
使用完文件后,需要使用 close()方法將其關閉
還可以結合 ofstream 對象和運算符<<來輸出各種類型的數據
注意:cout 控制臺輸入輸出中,頭文件 iostream 聲明了一個名為 cout 的 ostream 對象,無需自己手動聲明;文件輸出中,我們必須自己動手聲明一個 ofstream 對象,為其命名,將其同文件關聯起來。請看下面的例子:
#include<fstream> ofstream OutFile; //聲明一個 ofstream 對象 OutFile.open("study.txt"); //將OF與“study.txt”文件關聯起來
下面請看一個完整的程序,用戶輸入信息,將信息顯示到屏幕上,再將這些信息寫入到文本文件中:
#include <iostream> #include <fstream> using namespace std; int main() { char name[20]; double height; double weight; ofstream outFile;//創建了一個ofstream 對象 outFile.open("information.txt");//outFile 與一個文本文件關聯 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; //小數點格式顯示double outFile.precision(2); //設置精度 outFile.setf(ios_base::showpoint); //強制顯示小數點后的零 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和運算符>>來輸入各種類型的數據
文本文件輸入(讀取文本文件)
包含文件頭 fstream
頭文件 fstream 定義了一個ifstream 類用于處理輸出
聲明一個或多個 ifstream 對象,可以自由命名
指明名稱空間 std
把 3 中聲明的 ifstream 對象與文件關聯起來,比如使用 open()方法
使用完文件后,需要使用 close()方法將其關閉
還可以結合 ifstream 對象和運算符>>來讀取各種類型的數據
可以用 cin 和 get() 方法來讀取一個字符,或用 cin 和 getline() 方法來讀取一行字符
可以結合使用 ifstream 和 eof()、fail()方法來判斷輸入是否成功
如果試圖打開一個不存在的文件用于輸入將會導致后面使用 ifstream 對象讀取數據時發生錯誤,因此用戶需要使用 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 類型數據,并在控制臺輸出所讀取數據的數目、總和以及平均數:
#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; }
總結
原文鏈接:https://blog.csdn.net/francis_xd/article/details/78347595
相關推薦
- 2023-01-10 Go語言rune與字符串轉換的密切關系解析_Golang
- 2022-10-18 一文詳解Python中的Map,Filter和Reduce函數_python
- 2023-07-26 node中的內置模塊path和fs
- 2022-09-27 詳解adb工具的基本使用_Android
- 2022-11-13 PyTorch模型的保存與加載方法實例_python
- 2022-08-30 詳解Python單元測試的兩種寫法_python
- 2022-04-01 Kubernetes命令行工具--kubectl管理
- 2022-10-21 React?模式之純組件使用示例詳解_React
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支