[英]C++: std::ifstream ifs(path), 'path should be a constant'
我希望這樣的工作:
#include <iostream>
#include <fstream>
#include <string>
std::string path;
char c;
while (true) {
cin >> path;
std::ifstream ifs(path);
c = ifs.get();
while (ifs.good()) {
cout << c << endl;
c = ifs.get();
}
cout << endl;
}
它應該詢問路徑,然后寫出文件中的所有內容。 但是它說路徑應該是恆定的。 我該如何解決? 也許我應該更改我到達文件的方式? 謝謝。
您有兩種選擇(我知道):
std::ifstream ifs(path)
更改為std::ifstream ifs(path.c_str())
。 這是因為std::ifstream
構造函數將const char*
作為輸入,您可以使用std::string
類中的c_str()
方法從std::string
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.