标题:想问一下关于文件 函数的用法。。。
只看楼主
龙飞
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2007-6-8
 问题点数:0 回复次数:0 
想问一下关于文件 函数的用法。。。
这样子是可以运行的:
#include <iostream>
#include <fstream>
using namespace std;
void f()
{ofstream outfile("text.dat",ios::trunc);
if(!outfile)
{cout<<"open error!"<<endl;
exit(1);
}
for(int i=0;i<81;i++)
outfile<<i<<" ";
// outfile.close;
}
int main()
{void f();
f();
return 0;
}
而我想要把"text.dat"换成一个变量,也就是说想要把文件变成自己想要的任意名字。
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void f()
{string name;
cout<<"input the file name:";
cin>>name;
ofstream outfile(name,ios::trunc);
if(!outfile)
{cout<<"open error!"<<endl;
exit(1);
}
for(int i=0;i<81;i++)
outfile<<i<<" ";
// outfile.close;
}
int main()
{void f();
f();
return 0;
}
改完之后就没有办法编译通过了,不懂得是为什么,,
想请教一下,是否有办法实现我的这个想法,要的话,该怎么改才好?
搜索更多相关主题的帖子: 函数 用法 文件 outfile 
2007-06-11 02:30



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-146455-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.224413 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved