日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ofstream、ifstream、fstream

發布時間:2025/4/5 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ofstream、ifstream、fstream 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

    • 1 文件讀寫相關的流
      • 1.1 文件讀寫相關的流
      • 1.2 文件打開方式
    • 2 ofstream、ifstream、fstream
      • 2.1 ofstream和ifstream讀寫文本文件
      • 2.2 ofstream和ifstream讀寫二進制文件
      • 2.3 ofstream和ifstream對文件流按格式讀寫數據
      • 2.4 fstream
    • 3 文件流的狀態檢查
    • 4 文件流的定位
      • 4.1 seekg
      • 4.2 tellg
      • 4.3 seekp

1 文件讀寫相關的流

1.1 文件讀寫相關的流

文件流:對文件進行讀寫操作。

頭文件: <fstream>

類庫:

  • ifstream 對文件輸入(讀文件)
  • ofstream 對文件輸出(寫文件)
  • fstream 對文件輸入或輸出

1.2 文件打開方式


我們需要知道一點,對于是使用文本方式、還是二進制方式操作文件是無所謂的,重點在于我們采取何種方式解析文件。


2 ofstream、ifstream、fstream

2.1 ofstream和ifstream讀寫文本文件

通過ofstream寫文本文件:

#include <fstream> #include <iostream> #include <string>using namespace std;int main() {string name;int age;ofstream outfile; //也可以使用fstream, 但是fstream的默認打開方式不截斷文件長度// ofstream的默認打開方式是:截斷式寫入 ios::out | ios::trunc// fstream的默認打開方式是:非截斷式寫入 ios::out// 建議指定打開方式outfile.open("user.txt", ios::out | ios::trunc);while (1) {cout << "請輸入姓名: [ctrl+z退出] ";cin >> name;if (cin.eof()) { //判斷文件是否結束break;}outfile << name << "\t";cout << "請輸入年齡: ";cin >> age; outfile << age << endl; //文本文件寫入}// 關閉打開的文件outfile.close();system("pause");return 0; }

通過ifstream讀文本文件:

#include <fstream> #include <iostream> #include <string>using namespace std;int main() {string name;int age;ifstream infile;infile.open("user.txt");while (1) {infile >> name; // 使用這種方式讀取時,遇到空白字符會自動跳過(空格、制表符、回車換行符)if (infile.eof()) { //判斷文件是否結束break;}cout << name << "\t";infile >> age;cout << age << endl; }// 關閉打開的文件infile.close();system("pause");return 0; }

2.2 ofstream和ifstream讀寫二進制文件

思考:文本文件和二進制文件的區別?

  • 文本文件: 寫數字1, 實際寫入的是 ‘1’。
  • 二進制文件:寫數字1, 實際寫入的是 整數1(4個字節,最低字節是1, 高3個字節都是0)。寫字符‘R’實際輸入的還是‘R’。

使用文件流對象的write方法寫二進制文件:

#include <fstream> #include <iostream> #include <string>using namespace std;int main() {string name;int age;ofstream outfile;outfile.open("user.dat", ios::out | ios::trunc | ios::binary);while (1) {cout << "請輸入姓名: [ctrl+z退出] ";cin >> name;if (cin.eof()) { //判斷文件是否結束break;}outfile << name << "\t";cout << "請輸入年齡: ";cin >> age; //outfile << age << endl; //會自動轉成文本方式寫入outfile.write((char*)&age, sizeof(age));}// 關閉打開的文件outfile.close();system("pause");return 0; }

使用文件流對象的read方法讀二進制文件:

#include <fstream> #include <iostream> #include <string>using namespace std;int main() {string name;int age;ifstream infile;infile.open("user.dat", ios::in | ios::binary);while (1) {infile >> name;if (infile.eof()) { //判斷文件是否結束break;}cout << name << "\t";// 跳過中間的制表符char tmp;infile.read(&tmp, sizeof(tmp)); //infile >> age; //從文本文件中讀取整數, 使用這個方式infile.read((char*)&age, sizeof(age));cout << age << endl; //文本文件寫入}// 關閉打開的文件infile.close();system("pause");return 0; }

2.3 ofstream和ifstream對文件流按格式讀寫數據

使用stringstream按指定格式寫文件:

#include <fstream> #include <iostream> #include <string> #include <sstream>using namespace std;int main() {string name;int age;ofstream outfile;outfile.open("user.txt", ios::out | ios::trunc);while (1) {cout << "請輸入姓名: [ctrl+z退出] ";cin >> name;if (cin.eof()) { //判斷文件是否結束break;}cout << "請輸入年齡: ";cin >> age;stringstream s;s << "name:" << name << "\t\tage:" << age << endl;outfile << s.str();}// 關閉打開的文件outfile.close();system("pause");return 0; }

按指定格式讀文件:沒有優雅的C++解決方案, 使用C語言的sscanf。

#include <fstream> #include <iostream> #include <string> #include <sstream> #include <Windows.h>using namespace std;int main(void) {char name[32];int age;string line;ifstream infile;infile.open("user.txt");while (1) {getline(infile, line);if (infile.eof()) { //判斷文件是否結束break;}sscanf_s(line.c_str(), "姓名:%s 年齡:%d", name, sizeof(name),&age);cout << "姓名:" << name << "\t\t年齡:" << age << endl;}infile.close();system("pause");return 0; }

2.4 fstream

fstream的用法很簡單,就不描述了。


3 文件流的狀態檢查

s.is_open( ) :文件流是否打開成功。

s.eof( ) : 流s是否結束。

s.fail( )

  • 流s的failbit或者badbit被置位時, 返回true。
  • failbit: 出現非致命錯誤,可挽回, 一般是軟件錯誤。
  • badbit置位, 出現致命錯誤, 一般是硬件錯誤或系統底層錯誤, 不可挽回。

s.bad( )

  • 流s的badbit置位時, 返回true。

s.good( )

  • 流s處于有效狀態時, 返回true。

s.clear( )

  • 流s的所有狀態都被復位 。

4 文件流的定位

4.1 seekg

函數原型:seekg( off_type offset, ios::seekdir origin );

作用:設置輸入流的位置.

參數1: 偏移量。

參數2: 相對位置:

  • beg 相對于開始位置
  • cur 相對于當前位置
  • end 相對于結束位置

讀取當前程序的最后50個字符:

#include <iostream> #include <fstream> #include <string>using namespace std;int main(void) {ifstream infile;infile.open("定位.cpp");if (!infile.is_open()) {return 1;}infile.seekg(-50, infile.end);while (!infile.eof()) {string line;getline(infile, line);cout << line << endl;}infile.close();system("pause");return 0; }

4.2 tellg

返回該輸入流的當前位置(距離文件的起始位置的偏移量)。

獲取當前文件的長度:

#include <iostream> #include <fstream> #include <string>using namespace std;int main(void) {ifstream infile;infile.open("定位.cpp");if (!infile.is_open()) {return 1;}// 先把文件指針移動到文件尾infile.seekg(0, infile.end);int len = infile.tellg();cout << "len:" << len;infile.close();system("pause");return 0; }

4.3 seekp

設置該輸出流的位置。

先向新文件寫入:“123456789”,然后再在第4個字符位置寫入“ABC”:

#include <iostream> #include <fstream> #include <string>using namespace std;int main(void) {ofstream outfile;outfile.open("test.txt");if (!outfile.is_open()) {return 1;}outfile << "123456789";outfile.seekp(4, outfile.beg);outfile << "ABC";outfile.close();system("pause");return 0; }

參考資料:

  • C/C++從入門到精通-高級程序員之路【奇牛學院】
  • 總結

    以上是生活随笔為你收集整理的ofstream、ifstream、fstream的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。