[問題] c++ 如何用string當做讀檔檔名?

看板C_and_CPP (C/C++)作者 (Wesely翁)時間16年前 (2010/03/21 01:33), 編輯推噓2(201)
留言3則, 3人參與, 最新討論串1/1
遇到的問題: (題意請描述清楚) 我想開啟一個以string為檔名的檔案,我該怎麼做? -------------------------------------------------------- #include <iostream> #include <fstream> #include <string> using namespace std; string fname; // fname = "testfile" ifstream fin; cout << fname; //這裡輸出的話可以得到字串testfile fin.open(fname); -------------------------------------------------------- 希望得到的正確結果: 可以開啟"testfile"這個檔案 程式跑出來的錯誤結果: no matching function for call to `std::basic_ifstream<char, std::char_traits<char> >::open(std::string&)' 開發平台: (例: VC++ or gcc/g++ or Dev-C++, Windows or Linux) g++ -O0 -g3 我記得 fin.open(fname)的fname好像要什麼處理 試過fmane.begin()也沒有用 請大家幫幫忙了 >< -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.37.178.161 ※ 編輯: weselyong 來自: 114.37.178.161 (03/21 01:33) ※ 編輯: weselyong 來自: 114.37.178.161 (03/21 01:34)

03/21 01:37, , 1F
fname.c_str() ?
03/21 01:37, 1F

03/21 01:48, , 2F
樓上正解
03/21 01:48, 2F

03/21 02:45, , 3F
過了耶!!感謝一樓!!
03/21 02:45, 3F
文章代碼(AID): #1BfGRW_L (C_and_CPP)
文章代碼(AID): #1BfGRW_L (C_and_CPP)