美文网首页
C++文件操作

C++文件操作

作者: qratosone | 来源:发表于2016-05-02 00:26 被阅读0次

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

说明

假定文件流对象为 fstream fs;

读写模式打开文件时,需要使用 ios::in | ios::out 作为流对象fs的 open 函数的参数

判断文件操作(打开、读写)是否成功,使用 fs.fail() 判断。如果成功, fail() 返回 false,否则返回 true

   if (fs.fail()) {
        输出提示信息 
        用写模式打开文件
        关闭文件
        再次用读写模式打开文件
    }
  1. 打开文件后,如果文件为空(大小为0),那么从文件流里面读取数据时会失败。此时需要从键盘读取屏幕的宽和高。然后,需要调用下面的函数,清除文件流的状态位,否则所有后续文件操作都会失败。
    fs.clear()

  2. 移动文件写指针的函数是 seekp(),文件头的位置是 ios::beg。
    fs.seep(ios::beg) //将文件的写指针移动到文件头

  3. 移动文件的读指针的函数是 seekg()

相关文章

  • c++文件操作详解

    c++文件操作详解 C++ 通过以下几个类支持文件的输入输出: ofstream: 写操作(输出)的文件类 (由o...

  • 文件读写总结

    1. C++文件读写详解 1.1. 文件读写操作 使用方式 1.1.1. 打开文件 文件操作通过成员函数open(...

  • 2-ndk学习之c++基础篇(05)

    文件操作 首先是c语言读取文件:需要先添加头文件, 然后是c++读取文件:先添加头文件: 多线程入门 c++的多线...

  • C++文件操作

    说明 假定文件流对象为 fstream fs; 读写模式打开文件时,需要使用 ios::in | ios::ou...

  • C++ 文件操作

    C++ 文件和流 到目前为止,我们已经使用了 iostream 标准库,它提供了 cin 和 cout 方法分别用...

  • c++文件操作

  • 【C++】C++文件流操作

    文件操作是每门语言的必学项目,C++中同样如此,且C++中通过流(stream)这一形式进行输入输出、文件读写的控...

  • C++文件操作相关

    代码 说明 主函数中首先定义了string类型的文件名对象;然后创建了 fstream 的对象;随后调用open函...

  • c++ 文件操作大全

    转载(https://www.cnblogs.com/MrYuan/p/5383408.html) 基于C的文件操...

  • C++ 文件读写操作

    依赖gcc、 cmake 1. 代码编写 main.cpp 2. cmake 编写 CMakeLists.txt ...

网友评论

      本文标题:C++文件操作

      本文链接:https://www.haomeiwen.com/subject/mmclrttx.html