怎样输出到文件,如输出到文本文档.(C++)

HNLGDXYJS 2008-06-04 10:41:49
怎样输出到文件,如输出到文本文档.用C++语言.
比如,我想把"SDFGASSADFSGSDG"这些东西输出到文本文档并保存,要怎样写代码?
我知道要这个头文件(fstream)但我不知具体怎样用.麻烦大伙指点一下,谢谢了.
...全文
3260 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
highhigh_hi 2010-07-20
  • 打赏
  • 举报
回复
好东西
koulongfei 2009-05-09
  • 打赏
  • 举报
回复
学习了...........
HNLGDXYJS 2008-06-05
  • 打赏
  • 举报
回复
谢谢在家了,我好好看一下.
祝大家开心!
9527他大爷 2008-06-04
  • 打赏
  • 举报
回复
#include<fstream>
using namespace std;


int main()
{
ofstream outfile("abc.txt");
outfile<<"fdtgerqyeophy"<<endl;
outfile.close();


return 0;
}
五号智能 2008-06-04
  • 打赏
  • 举报
回复

include<iostream>
#include<fstream>

usingnamespacestd;

intmain()
{
string a="SDFGASSADFSGSDG" ;
ofstream outfile("abc.txt");
outfile<<a<<endl;
outfile.close();
return0;
}

方法2

class Hotel
{
private:
int No;
string name;
string cty;
int total_room;
int proom;
int proom_empty;
double proom_money;
int hroom;
int hroom_empty;
double hroom_money;


public:




// 运算符重载决定了写到文件的文本格式

friend ostream&
operator << (ostream &out,Hotel &hot)
{
out <<hot.No<<" "<<hot.name<<" "<<hot.cty<<" "<<hot.total_room<<" "<<hot.proom
<<" "<<hot.proom_empty<<" "<<hot.proom_money<<" "<<hot.hroom
<<" "<<hot.hroom_empty<<" "<<hot.hroom_money<<endl;

return out;
}

friend istream&
operator >> (istream &in,Hotel &hot)
{
in >> hot.No >> hot.name >> hot.cty >> hot.total_room >>hot.proom
>> hot.proom_empty >> hot.proom_money >>hot.hroom
>> hot.hroom_empty >> hot.hroom_money;
return in;
}
};


int loadfile(list<Hotel> &list_hot) //读文件
{
ifstream infile("hot.dat");

while (infile >> temp)
{
temp.num(num);
list_hot.push_back(temp);
num++;
}
infile.close();
infile.clear();
}

void savefile(list<Hotel> &list_hot) //写文件
{
ofstream outfile("hot.dat");

for(list<Hotel>::iterator li_hot=list_hot.begin();li_hot!=list_hot.end();li_hot++)
{
outfile<<*li_hot;
}
outfile.close();
outfile.clear();
}


lijiawlm 2008-06-04
  • 打赏
  • 举报
回复
用ofstream


接分来了
HelloDan 2008-06-04
  • 打赏
  • 举报
回复

#include<iostream>
#include<fstream>
using namespace std;


int main()
{
ofstream outfile("abc.txt");
outfile<<"fdtgerqyeophy"<<endl;
outfile.close();


return 0;
}



wuyu637 2008-06-04
  • 打赏
  • 举报
回复
#include <fstream>

void main()
{
ofstream outfile("test.txt");
outfile << "hello world" << endl;
outfile.close();
}

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧