2011-05-04 96 views
0

我詢問在第K行尋找文本文件中的函數,以及在C++中按行或字符讀取文本文件的函數!知道我正在與borland合作。C++ textfile borland

+0

http://www.cplusplus.com/doc/tutorial/files/ – 2011-05-04 19:41:50

+0

這將有助於更清楚地瞭解您詢問的內容。有標準的功能來讀取文件中的字符或文本文件中的一行。在文本文件中沒有找到第K行的標準函數。你在問怎麼寫?這是功課嗎? – 2011-05-04 19:45:32

+0

好的,謝謝!但你能告訴我如何處理文本文件中的指針來提取信息嗎? – samia 2011-05-04 19:46:11

回答

1

fpeek是一個開源應用程序,完全可以做到這一點。檢查來源,看看它是如何完成的。

我剛剛看了一下,我相信你會是這樣結束了(我沒有測試此代碼):

std::ifstream file(filename); 

std::string line; 
int pos = 1; 
while (std::getline(file, line)) 
{ 
    // Find if current line should be displayed 
    if (15 == pos) // looking for the 15th line in the file 
    { 
     std::cout << pos << ": " << line << std::endl; 
    } 

    pos++; 
} 
+0

文本文件中第一行的索引是什麼?它從0還是1開始? – samia 2011-05-04 20:00:40

+0

@samia更新了答案。 – karlphillip 2011-05-04 20:01:14

+0

std :: getline()從文件的第一行開始讀取。 – karlphillip 2011-05-04 20:02:10