我在這裏掙扎了一下,我有一些代碼類似於此:C++:如何請求一個文件名並打開它?
#include <iostream>
#include <string>
#include <ifstream>
using namespace std;
int main() {
string filename;
cout << "Enter the filename: ";
cin >> filename;
ifstream myfile(filename);
//code that does stuff, eg:
while (myfile >> thing) {
mydata[i] = thing;
i = i + 1; //etc, this *isn't* an issue
}
}
的主要問題是,它似乎並沒有與克至++編譯,但我敢肯定,之前我有這個工作。我讀的地方這是string filename;
位是被打破了,但我不知道該怎麼要求比把它作爲一個string
其他的文件名。誰能幫忙?我也應該使用<fstream>
或<ifstream>
我真的沒有看到區別。謝謝。
'CIN >> >>名ENDL;'爲什麼你想推的東西'的std :: endl'? – Mat
哦,呃,我不知道,那不是問題,那是我快速打字。 –
如果'filename'包含空格,則使用'getline()'。 – herohuyongtao