更改变量目录的 .txt 文件
我正在尝试编写一个程序来更改变量目录的 .txt 文件。 但是,当我尝试这样做时,我收到错误。
我的错误:
no matching function for call to `std::basic_ofstream<char, std::char_traits<char> >::basic_ofstream(std::string&)'
我的代码:
#include <iostream>
#include <fstream>
using namespace std;
int main () {
string file_name ;
cout << "Input the directory of the file you would like to alter:" << endl;
cin >> file_name ;
ofstream myfile ( file_name );
if (myfile.is_open())
{
myfile << "This is a line.\n";
myfile << "This is another line.\n";
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
我做错了什么?
I'm trying to write a program that alters a .txt file of a variable directory.
However when I attempt to do so I get an error.
My error:
no matching function for call to `std::basic_ofstream<char, std::char_traits<char> >::basic_ofstream(std::string&)'
My code:
#include <iostream>
#include <fstream>
using namespace std;
int main () {
string file_name ;
cout << "Input the directory of the file you would like to alter:" << endl;
cin >> file_name ;
ofstream myfile ( file_name );
if (myfile.is_open())
{
myfile << "This is a line.\n";
myfile << "This is another line.\n";
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
What did I do wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
fstream
构造函数采用 C 字符串,而不是std::string
,因此您需要它们采用 C 字符串而不是
std::string
的原因s 纯粹是历史性的;没有技术原因。 C++0x 添加了采用 std::string 的构造函数。The
fstream
constructors take C strings, notstd::string
, so you needThe reason why they take C strings instead of
std::string
s is purely historical; there's no technical reason. C++0x adds constructors that takestd::string
s.