2011-09-25 77 views
0

這是打開文件輸入的正確方法嗎?這是打開輸入文件的正確方法嗎?

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構造函數。

我的編譯器出錯了,或者我只是在我的參數中使用了錯誤的類型?

回答

2

不使用指針。這裏不需要。

試試這個:

ifstream file(path); 

,然後用它作爲:

//... 
file.getline(buffer, line_number);//file->getline(buffer, line_number); 
//... 
+0

我想已經:\。錯誤仍然是一樣的,告訴我我不能將路徑轉換爲ifstream對象或其他東西。 – zeboidlund

+2

@荷蘭,你有沒有'#include ',並且在你嘗試時使用'std :: ifstream'? –

+0

是的,出於某種原因現在它可以工作,但無論什麼原因,當我測試它。我得到一個std :: bad_alloc運行時錯誤。這裏是我的主代碼: std :: string bs [] = {「test」,「test2」,「test3」}; BinaryTree tree(bs,1); tree.read(「questions.txt」,1); – zeboidlund

0

沒有指針需要,爲@Nawaz說:

ifstream *file(path); 

潛在的內存泄漏:

char *buffer = new char[length]; 

你應該事後delete[]它:

delete[] buffer; 

... ,它是一個更容易使用std::string

std::string buffer; 

最終代碼:

std::string BinaryTree::read(std::string path, int line_number) 
{ 
    std::string buf; 
    ifstream file(path.c_str()); 

    if(file.is_open()) 
    { 
     // file.seekg(0, std::ios::beg); // @Tux-D says it's unnecessary. 
     file.getline(buf, line_number); 
     file.close(); 
    } 

    return buf; 
} 
+0

除非他使用C++ 11,否則ifstream不接受std :: string,但只能使用char * –

+0

我正在使用QT創建器 - 我不確定是否使用C++ 11。 – zeboidlund

+0

@Holland我修改了代碼以使用良好的'C++ 03'。你真的應該修復內存泄漏。 –

1
ifstream * file(path); //error: cannot convert ‘char*’ to ‘std::ifstream*’ in initialization 

問題是該對象的構造不合適。你可能會嘗試做以下(或類似的東西),實際上傳遞一個字符數組的ifstream的對象的構造函數:

ifstream file(path); 

然而,引入一個星號的位置改變了全部意義。您正在創建一個指向對象ifstream的指針,但不是對象ifstream本身。而爲了構建一個指針,你需要另一個指向ifstream對象的指針(即一個相同類型的指針)。

ifstream file(path); 
ifstream * ptr(&path); 

這是不是你打算做,反正,你可能想創建一個指針引用一個ifstream對象:

ifstream * file = new ifstream(path); 
//... more things... 
file->close(); 

但請記住,當它是對象必須free'd不再需要了。由指針引用的對象不會像普通(堆棧中的對象)對象那樣自動釋放。

ifstream * file = new ifstream(path); 
//... more things... 
file->close(); 
delete file; 

希望這會有所幫助。

0

的東西夫婦我會改變:

void BinaryTree::read(char * path, int line_number) 
{ 
    // Use an object not a pointer. 
    ifstream*  file(path); 

    // When you open it by default it is at the beginning. 
    // So we can remove it. 
    file->seekg(0, std::ios::beg); 

    // Doing manually memory line management. 
    // Is going to make things harder. Use the std::string 
    int length = file.tellg(); 
    char * buffer = new char[length]; 
    file.getline(buffer, line_number); 

    // Printing the line use the C++ streams. 
    printf("%d", length); 

    // DO NOT manually close() the file. 
    // When the object goes out of scope it will be closed automatically 
    // http://codereview.stackexchange.com/q/540/507 
    file->close(); 

} // file closed here automatically by the iostream::close() 

這裏簡化爲:

void BinaryTree::read(char * path, int line_number) 
{ 
    ifstream  file(path); 

    std::string line; 
    std::getline(file, line); 

    std::cout << line.size() << "\n"; 
} 
0

你有比上面雖然提到的那些問題比較多:

  • 你使用文件作爲指針和對象(file-> seekg(...)和file.tellg())
  • 在使用seekg移動到文件開頭並使用該位置爲您提供緩衝區大小後,您正在使用tellg。這會給你一個空的緩衝區(最好是指向零字節的指針,我認爲)。
  • 然後,您使用作爲參數傳遞的line_number參數調用getline。這會導致getline讀取最多的line_number字節,但是您只分配了長度字節(如上所示,它爲零)。我想你想讀第line_number行,但需要更多的工作 - 你必須計算line_number換行符,直到你到達正確的位置。
  • 更爲普遍的是,您希望在每次獲得下一行時打開和關閉文件 - 您可能希望重新考慮整個界面。

道歉,如果我錯過了這裏的點

相關問題