【发布时间】:2013-11-17 11:51:59
【问题描述】:
我正在编写一个函数,让用户有机会更改他们的用户名。为此,我试图重命名存储数据的文件。我想出了一种方法,虽然不太奏效,但我认为我已经接近了。
文件名最初是这样创建的 -
std::cout << "Please enter a username: ";
std::getline (std::cin, username);
std::ofstream fout (username + ".txt");
效果很好。然后如果他们以后选择更改用户名
std::cout << "Please type in your new username." << std::endl;
std::getline (std::cin, newUsername);
std::ofstream fout (newUsername + ".txt");
// I copied the contents of username.txt to newUsername.txt here
这又可以正常工作了。
问题出在下面。
问题在于删除原始文件,更具体地说是添加文件扩展名.txt时
我已经为 remove() 功能添加了#include 并将 .c_str() 添加到用户名,因为我相信 remover () 只会采用 C 字符串(char*),而不是 C++ 字符串。
remove(username.c_str() + ".txt"); // error expression must have integral or enum type referring to ".txt".
提前感谢大家的时间
【问题讨论】: