我正在尝试从用户通过stdin获取字符串并将其保存到变量InputString
,然后创建一个与InputString
的值同名的二进制文件。这是我到目前为止编写的代码:
std::string InputString;
getline(std::cin, InputString);
std::cout << InputString << std::endl;
// The code above works.
// Errors start below. :(
void Printi(std::string filename)
{
std::ofstream Printi(filename".bin");
Printi((char*)&Hans, sizeof(Person)); // Hans is an instance of my class Person.
Printi.close();
}
Printi(InputString);
我收到以下错误(从我的本地化编译器翻译成英文):
"Printi": Local function definition is not allowed
Missing ")" (in line std::ofstream Printi..)
如何仅使用标准C ++库来解决此问题?
答案 0 :(得分:2)
std::ofstream Printi(filename".bin")
需要std::ofstream Printi(filename + ".bin")
。 +运算符用于连接字符串并将.bin附加到文件名中提供的结尾。