簡體   English   中英

C ++:std :: ifstream ifs(path),“路徑應為常量”

[英]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;
}

它應該詢問路徑,然后寫出文件中的所有內容。 但是它說路徑應該是恆定的。 我該如何解決? 也許我應該更改我到達文件的方式? 謝謝。

您有兩種選擇(我知道):

  1. 使用c ++ 11(添加std = c ++ 11編譯標志)
  2. 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM