C++文件操作相关

代码

#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:
    //----补充多个数据域成员
    unsigned int _width;
    unsigned int _height;

    // 在Screen类中获取/释放图形窗口资源,是一种RAII方法
    //   关于RAII,可以参见异常处理单元的材料
    Screen(unsigned int width, unsigned int height) {
        // 如果启用了图形库,则将初始化图形模式的函数置于此处
        // initgraph(width_, height_);

        _width = width;
        _height = height;

    };
    Screen(){

    }
    ~Screen () {
        // 如果启用了图形库,则将关闭图形模式的函数置于此处
        // closegraph();
        delete instance;
    }

public:
    static Screen* instance;
    //----补充 getWidth() 与 getHeight() 函数,

    static Screen* getInstance(unsigned int width = 640, unsigned int height = 480) {
        // 单例模式
        //----补充函数体
        if (instance == 0) {
            instance = new Screen(width, height);
        }
        return instance;
    }
    unsigned int getWidth(){
        return _width;
    }

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

Screen* Screen::instance = 0;
//----补充Screen类的特殊数据成员初始化语句

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编译器在调试模式下会暂停,便于查看运行结果
#if ( defined(__DEBUG__) || defined(_DEBUG) )
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    cin.get();
#endif

    return 0;
}

说明

主函数中首先定义了string类型的文件名对象;
然后创建了 fstream 的对象;
随后调用open函数,使用读写模式打开文件。

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

通过使用类似下面的代码(fail()函数),对文件打开的状态进行判别。
对于使用 ios::in | ios::out 模式打开的文件,如果打开失败,一般来说是文件不存在(也有可能是文件是不可写的)
如果 fail() 函数返回真值,则创建该文件(用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);
    }

从文件中读入屏幕宽和高,如果读取失败,则清除文件操作的状态位,然后从键盘读入屏幕宽和高

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

移动文件的输出指针到文件头,然后将屏幕的宽和高写入到文件中。
如果写失败,则关闭文件并且返回 -1,结束程序。

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

移动文件的输入指针到文件头,然后从文件中读出将屏幕的宽和高,再将其显示到屏幕上。
最后关闭文件流。

    fs.seekg(ios::beg);
    fs >> width >> height;
    cout << width << " " << height << endl;
    fs.close();
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • C/C++输入输出流总结 前两天写C++实习作业,突然发现I/O是那么的陌生,打了好长时间的文件都没有打开,今天终...
    LuckTime阅读 1,753评论 0 6
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,257评论 4 61
  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,217评论 30 472
  • c++文件操作详解 C++ 通过以下几个类支持文件的输入输出: ofstream: 写操作(输出)的文件类 (由o...
    鲍陈飞阅读 1,809评论 0 2
  • 昨天晚上,我和全国的家长朋友一起聆听了爱、自然、生命力体系的一节微课《孩子爱哭、胆小怎么办?》 对于这样一个话题,...