Chapter 10 輸入輸出流 (reading notes)

0. 版權聲明

  • C++ 系列讀書筆記來源於 Bjarne Stroustrup 所著《C++程序設計原理與實踐(基礎篇)》;1
  • 該系列筆記不以盈利爲目的,僅用於記錄學習過程中的知識要點和心得體會;
  • 如有侵權,請與本人聯繫([email protected]),經覈實後即刻刪除;
  • 轉載請註明出處;

1. 打開文件

1.1 concept

  • 與文件相關聯的流:
    • ifstream;
    • ofstream;
    • fstream;
  • 文件流離開作用域時,與之關聯的文件被關閉;
    • 在創建 ifstream 和 ofstream 對象時隱式地打開文件;
    • 依靠流對象的作用域關閉文件;

1.2 code

cout << "Please enter input file name:";
string iname;
cin >> iname;
ifstream ist {iname};
if (!ist)
    error("can't open input file ", iname);
  • 文件流與文件相關聯,方可使用;

  1. Bjarne S. C++程序設計原理與實踐(基礎篇)[M]. 任明明,王剛,李忠偉, 譯. 北京: 機械工業出版社, 2017. ↩︎

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章