请高手看一下我写的c++函数
int create()
{string s,name;
char fn= 'n ';
cout < < "请输入文件名: " < <endl;
cin> > name;
ofstream outfile( "name ",ios::out);
while((fn== 'n ')||(fn== 'N '))
{cout < < "输入一行文本: " < <endl;
cin> > s;
outfile < <s < <endl;
cout < < "结束输入吗? y or n ? " < <endl;
cin> > fn;
}
cout < < "文件建立成功! " < <endl;
return 0;
}
ofstream outfile( "name ",ios::out);如果这样就不能创建文件,但编译不出错。如果改成 ofstream outfile( "f1.txt ",ios::out);就能成功。
请问怎么实现从键盘上输入文件名?
[解决办法]
ofstream outfile( "name ",ios::out);
==>
ofstream outfile(name,ios::out);
[解决办法]
#include <string>
#include <iostream>
using namespace std;
void main( )
{ string word;
while(cin> > word){
word+= " ";
cout < <word;
}
}