2010-06-30 48 views
1

我必須從extern文本文件中讀取行,並且需要某些行的1.字符。seekg tellg行尾

是否有一個功能,它可以告訴我,在哪一行的指針和其他功能,它可以將指針設置爲開始直線x的?

我必須跳轉到當前位置前後的行。

回答

0

我覺得沒有這樣的功能。您必須自己使用getline()自己來實現此功能,或者一次掃描文件中的末尾行字符(\n)一個字符並存儲該行之後的一個字符。

您可能會發現一個向量(vector<size_t>可能)有助於存儲行開頭的偏移量,這樣您可以以基於行的方式跳轉到文件中。但還沒有嘗試過,所以它可能無法正常工作。

0

您可以看看ifstream以在流中讀取您的文件,然後使用getline()獲取std::string中的每一行。

這樣做,你可以輕鬆地重複線槽線和搶你所需要的字符。

這裏是(來自here截取)的示例:

// reading a text file 
#include <iostream> 
#include <fstream> 
#include <string> 
using namespace std; 

int main() { 
    string line; 
    ifstream myfile ("example.txt"); 
    if (myfile.is_open()) 
    { 
    while (! myfile.eof()) 
    { 
     getline (myfile,line); 
     cout << line << endl; // Here instead of displaying the string 
     // you probably want to get the first character, aka. line[0] 
    } 
    myfile.close(); 
    } 

    else cout << "Unable to open file"; 

    return 0; 
}