這是打開文件輸入的正確方法嗎?這是打開輸入文件的正確方法嗎?
void BinaryTree::read(char * path, int line_number)
{
ifstream * file(path); //error: cannot convert ‘char*’ to ‘std::ifstream*’ in initialization
file->seekg(0, std::ios::beg);
int length = file.tellg();
char * buffer = new char[length];
file->getline(buffer, line_number);
printf("%d", length);
file->close();
}
我猜沒有,因爲編譯器不會接受char
陣列,或爲ifstream
構造一個std::string
,但是當我讀到documentation,我看到string
S和/或char
陣列傳遞給ifstream
構造函數。
我的編譯器出錯了,或者我只是在我的參數中使用了錯誤的類型?
我想已經:\。錯誤仍然是一樣的,告訴我我不能將路徑轉換爲ifstream對象或其他東西。 – zeboidlund
@荷蘭,你有沒有'#include',並且在你嘗試時使用'std :: ifstream'? –
是的,出於某種原因現在它可以工作,但無論什麼原因,當我測試它。我得到一個std :: bad_alloc運行時錯誤。這裏是我的主代碼: std :: string bs [] = {「test」,「test2」,「test3」}; BinaryTree tree(bs,1); tree.read(「questions.txt」,1); – zeboidlund