我想製作一個簡單的程序,允許用戶創建/打開文件並向它們添加文本。這是代碼我目前:如何根據用戶輸入指定的文件名打開文件?
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
cout << "Enter file name:" << endl;
char fileName;
cin >> fileName;
ofstream myFile;
myFile.open(fileName, ios::out);
myFile << "This is the file text.\n";
myFile.close();
return 0;
}
我收到的myFile.open(fileName, ios::out)
以下錯誤:
error: no matching function for call to 'std::basic_ofstream<char>::open(std::__cxx11::string&, const openmode&)'
文件名應爲字符串 –
它不會是有趣的,只允許1個字符的文件名.. – drescherjm
@ Raindrop7咩! –