的基本語法我用寫一個.txt文件是這樣的:寫在用戶定義的.txt文件
ofstream myfile;
myfile.open ("data.txt", ios::trunc);
outfile<<"writing";
現在,假設我是讓用戶決定哪些文件應該被訪問,它可以通過一個字符串來完成嗎?
#include <iostream>
#include <fstream>
using namespace std;
int main(){
ofstream myfile;
string filename;
char x;
cin>>x;
if (x==1)
filename="data2.txt";
myfile.open (filename, ios::trunc);
myfile<<"writing";
return 0;
}
我已經測試過了,它沒有工作。我的問題是:是否有可能做這樣的事情?如果是,如何?當我編譯它,我得到的錯誤是:
undefined reference to 'std::basicofstream<char, std::char_traits<char> >::open(std::string const&, std::_Ios_Openmode)'
我不明白是什麼導致它。
指定「沒有工作」。沒有編譯?如果是這樣,請發佈錯誤。沒有鏈接?如果是這樣,請發佈錯誤。沒有按預期行事?如果是這樣,發佈預期和觀察行爲。 – Angew
你說得對,我很抱歉。我編輯它,還包括整個代碼。 –
什麼是完整的錯誤信息? – melpomene