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,可以參見(jiàn)異常處理單元的材料
    Screen(unsigned int width, unsigned int height) {
        // 如果啟用了圖形庫(kù),則將初始化圖形模式的函數(shù)置于此處
        // initgraph(width_, height_);

        _width = width;
        _height = height;

    };
    Screen(){

    }
    ~Screen () {
        // 如果啟用了圖形庫(kù),則將關(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ù)成員初始化語(yǔ)句

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)試模式下會(huì)暫停,便于查看運(yùn)行結(jié)果
#if ( defined(__DEBUG__) || defined(_DEBUG) )
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    cin.get();
#endif

    return 0;
}

說(shuō)明

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

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

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

    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)位,然后從鍵盤(pán)讀入屏幕寬和高

    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;
    }

移動(dòng)文件的輸出指針到文件頭,然后將屏幕的寬和高寫(xiě)入到文件中。
如果寫(xiě)失敗,則關(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;
    }

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

    fs.seekg(ios::beg);
    fs >> width >> height;
    cout << width << " " << height << endl;
    fs.close();
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

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

友情鏈接更多精彩內(nèi)容