关于c++的IO流的写文件

来源:百度知道 编辑:UC知道 时间:2024/06/06 03:29:30
怎样写一个文件啊?
我只会读文件
比如:
ifstream& open_file(ifstream& in,const string &file)
{
in.close();
in.clear();
in.open(file.c_str());
return in;
}

ifstream& print(ifstream& in,const string &file)
{
string std;
while(in>>std)
cout<<std<<endl;
in.close();
return in;
}
怎样在程序中建立并写入一个文件呢?
比如:在程序建立一个名为"nana"的文档
写入“nana is a dog!”

请附源代码
如果可以运行的的话我再加100分!

// Test.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <fstream>
using namespace std;

void inFile();

ifstream infile;
ofstream outfile;
char name[30],a,str[1024];

float b;

int _tmain(int argc, _TCHAR* argv[])
{
cout<<"请你选择要进行的操作:"<<endl;
cout<<"1读取文件!"<<" "<<"2创建和写入文件!"<<" "<<"3删除文件!"<<endl;
cin>>b;
if (b==1.0f)
{
inFile();
if (!infile)
{
cout<<"未找到名称为 "<<name<<" 的文件!!"<<endl;
inFile();
}else
{
cout<<"成功找到文件<<"<<name<<">>内容如下:"<<endl;
}

while (infile.get(a))
{
cout<<a;
}
cout&l