文件IO操作之打开与关闭

7、小作业:
咱们课堂上主要是使用 ifstream 和 ofstream 对象来进行文件的操作,大家课后请使用 fstream 文件流来对文件进行读写操作,把课堂上讲解的知识点实现一遍。

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
    fstream file;
    file.open("d://123.txt",ios::out);
    if (file.fail())
    {
        cout << "错误" << endl;
    }
    string text = "fsesgf";
    file << text;
    //int text = 20000;
    //file.write((char*)&text, sizeof(int));    二进制写入
    file.close();
    file.open("d://123.txt", ios::in);
    if (!file.good())
    {
        cout << "错误" << endl;
    }
    text = 88;
    file >> text;        
    //file.read((char*)&text, sizeof(int));    二进制读取
    cout << text << endl;
    file.close();
    return 0;
}

 

posted on 2021-01-11 20:12  SakuraQAQ  阅读(221)  评论(0)    收藏  举报

导航