C++文件操作相關(guān)

代碼

#include <iostream>
#include <limits>
#include <cstdlib>
#include <fstream>
#include <string>
using namespace std;
void exitWhenInvalidScreen(int input) {
    if (input <= 0 || input>1000) {
        std::cout << "invalid screen size" << std::endl;
        exit(0);
    }
}
class Screen {
private:
    //----補(bǔ)充多個(gè)數(shù)據(jù)域成員
    unsigned int _width;
    unsigned int _height;

    // 在Screen類中獲取/釋放圖形窗口資源,是一種RAII方法
    //   關(guān)于RAII,可以參見異常處理單元的材料
    Screen(unsigned int width, unsigned int height) {
        // 如果啟用了圖形庫,則將初始化圖形模式的函數(shù)置于此處
        // initgraph(width_, height_);

        _width = width;
        _height = height;

    };
    Screen(){

    }
    ~Screen () {
        // 如果啟用了圖形庫,則將關(guān)閉圖形模式的函數(shù)置于此處
        // closegraph();
        delete instance;
    }

public:
    static Screen* instance;
    //----補(bǔ)充 getWidth() 與 getHeight() 函數(shù),

    static Screen* getInstance(unsigned int width = 640, unsigned int height = 480) {
        // 單例模式
        //----補(bǔ)充函數(shù)體
        if (instance == 0) {
            instance = new Screen(width, height);
        }
        return instance;
    }
    unsigned int getWidth(){
        return _width;
    }

    unsigned int getHeight(){
        return _height;
    }
};

Screen* Screen::instance = 0;
//----補(bǔ)充Screen類的特殊數(shù)據(jù)成員初始化語句

int main() {
    int width, height;
    Screen* screen = 0;
    string filename="screen.txt";
    fstream fs;
    fs.open(filename,ios::out|ios::in);
    if(fs.fail()){
        cout<<"open failed!"<<endl;
        fs.open(filename,ios::out);
        fs.close();
        fs.open(filename,ios::out|ios::in);

    }
    fs>>width>>height;
 //   cout<<width<<" "<<height<<endl;
    if(fs.fail()){
        cout<<"reading failed!"<<endl;
        cin>>width>>height;
    }
    fs.clear();


    screen = Screen::getInstance(width, height);
    screen = Screen::getInstance();
    fs.seekp(ios::beg);
    fs <<screen->getWidth() << " " <<screen->getHeight();
    fs.clear();
    fs.seekg(ios::beg);
    int getwidth,getheight;
    fs>>getwidth>>getheight;
    cout<<getwidth<<" "<<getheight<<endl;
    fs.close();
// GCC及VC編譯器在調(diào)試模式下會暫停,便于查看運(yùn)行結(jié)果
#if ( defined(__DEBUG__) || defined(_DEBUG) )
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    cin.get();
#endif

    return 0;
}

說明

主函數(shù)中首先定義了string類型的文件名對象;
然后創(chuàng)建了 fstream 的對象;
隨后調(diào)用open函數(shù),使用讀寫模式打開文件。

string fn("screen.txt");
fstream fs;
fs.open(fn, ios::in | ios::out);

通過使用類似下面的代碼(fail()函數(shù)),對文件打開的狀態(tài)進(jìn)行判別。
對于使用 ios::in | ios::out 模式打開的文件,如果打開失敗,一般來說是文件不存在(也有可能是文件是不可寫的)
如果 fail() 函數(shù)返回真值,則創(chuàng)建該文件(用ios::out模式);
然后再次使用 **ios::in | ios::out **模式打開該文件。

    if (fs.fail()) {
        cout << "file does not exist." << endl;
        fs.open(fn, ios::out);
        fs.close();
        fs.open(fn, ios::in | ios::out);
    }

從文件中讀入屏幕寬和高,如果讀取失敗,則清除文件操作的狀態(tài)位,然后從鍵盤讀入屏幕寬和高

    fs >> width >> height;
    if (fs.fail()) {
        cout << "can not read from file" << endl;
        cout << "Please input screen width and height:" << endl;
        fs.clear();
        cin >> width >> height;
    }

移動文件的輸出指針到文件頭,然后將屏幕的寬和高寫入到文件中。
如果寫失敗,則關(guān)閉文件并且返回 -1,結(jié)束程序。

    fs.seekp(ios::beg);
    fs << screen->getWidth() << " " << screen->getHeight() << endl;
    if (fs.fail()) {
        cout << "Can not write to file, exit" << endl;
        fs.close();
        return -1;
    }

移動文件的輸入指針到文件頭,然后從文件中讀出將屏幕的寬和高,再將其顯示到屏幕上。
最后關(guān)閉文件流。

    fs.seekg(ios::beg);
    fs >> width >> height;
    cout << width << " " << height << endl;
    fs.close();
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • C/C++輸入輸出流總結(jié) 前兩天寫C++實(shí)習(xí)作業(yè),突然發(fā)現(xiàn)I/O是那么的陌生,打了好長時(shí)間的文件都沒有打開,今天終...
    LuckTime閱讀 1,752評論 0 6
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,246評論 4 61
  • *面試心聲:其實(shí)這些題本人都沒怎么背,但是在上海 兩周半 面了大約10家 收到差不多3個(gè)offer,總結(jié)起來就是把...
    Dove_iOS閱讀 27,211評論 30 472
  • c++文件操作詳解 C++ 通過以下幾個(gè)類支持文件的輸入輸出: ofstream: 寫操作(輸出)的文件類 (由o...
    鮑陳飛閱讀 1,809評論 0 2
  • 昨天晚上,我和全國的家長朋友一起聆聽了愛、自然、生命力體系的一節(jié)微課《孩子愛哭、膽小怎么辦?》 對于這樣一個(gè)話題,...