C++ ofstream学习

转自:https://blog.csdn.net/kingstar158/article/details/6859379

1.介绍

#include <fstream>
ofstream         //文件写操作 内存写入存储设备 
ifstream         //文件读操作,存储设备读区到内存中
fstream          //读写操作,对打开的文件可进行读写操作 

 打开文件的方式在ios类(所以流式I/O的基类)中定义,有如下几种方式:

 例子:

#include <iostream>
#include <time.h>
#include <fstream>

using namespace std;

int main()
{
    ofstream outfile;
    string file="./email.txt";
    outfile.open(file,ios::trunc|ios::out);
    outfile<<"bbb\n";
    outfile.close();
    return 0;
}

验证结果,确实会删掉原来的文件,然后重新写入。 

 

posted @ 2022-11-04 19:43  lypbendlf  阅读(297)  评论(0编辑  收藏  举报