c++写文件

 

 

#include<iostream>
#include<fstream>//fstream标识文件流
using namespace std;
int main()
{
    ofstream outfile;//ofstream: out file stream
    outfile.open("myfile.txt");//ofstream类的对象调open()函数,把对象和文件关联
    outfile << "一片花飞减却春,风飘万点正愁人。\
                且看欲尽花经眼, 莫厌伤多酒入唇。\
                江上小堂巢翡翠, 苑边高冢卧麒麟。\
                细推物理须行乐, 何用浮名绊此身。\
                朝回日日典春衣, 每日江头尽醉归。\
                酒债寻常行处有, 人生七十古来稀。\
                穿花蛱蝶深深见, 点水蜻蜓款款飞。\
                传语风光共流转, 暂时相赏莫相违。" << endl;//利用outfile对象把数据输出到文件myfile.txt中;
    outfile.close();//输出完成后关闭对象与文件之间的联接
    system("pause");
    return 0;
}
View Code

 

posted on 2019-03-25 14:40  一杯明月  阅读(179)  评论(0编辑  收藏  举报