2013-02-26 44 views
1

我有以下模式一些.txt文件:爲了通過C++ fstream的分離在.txt文件註釋和數據

#Some comments here 

bull rocket 3 
trailer laker -12 

#More comments there 

Warriors Pacers 9 

基本上,有一些評論周圍以#開始 等線路包含兩個串接着一個int

我需要這兩個字符串和一個INT逐一處理 ,我不得不忽略任何空白行或行,從第

我想用ifstream.get()閱讀杉木st字符和 如果它是一個#

但我在卡住了,當涉及到數據。如何讀取char 然後獲取第一個字符串?即我找到'b',然後我需要「公牛」。我該怎麼辦?

感謝

回答

2
#include <iostream> 
#include <fstream> 
#include <sstream> 
#include <string> 
using namespace std; 
int main() 
{ 
     fstream f1("f1.data"); 
     string line,w1,w2; 
     int num; 

     while (getline(f1,line)) { 
       if (istringstream(line) >> w1 >> w2 >> num 
        && w1[0] != '#') 
         cout << w1 <<' '<< w2 <<' '<< num << '\n'; 
     } 
} 

這是輕量級的文本掃描,而不是詞法分析器JavaScript解釋或一些這樣的;清晰度勝過所有事情,所以使用C++的部分腳本語言來充分利用它。

+0

這個效果很好。謝謝 – dunfa 2013-02-26 17:06:57

+0

np,我剝去了一個不需要的溫度。 – jthill 2013-02-26 17:24:05

3

使用 「while (std::getline(is, line)) {」 來讀取文件流(std::istream is)在時間線(std::string line)。

如果lineempty()或開始於#,continue。執行此檢查之前,您可能希望修剪任何前導空格。

否則,解析該行(可能使用std::istringstream iss(line); iss >> first >> second >> value;)。在StackOverflow的其他地方有很多很棒的例子來說明如何做到這一點。

1

示例代碼:

ifstream in("file.txt"); 
    if (!in.is_open()) 
     return false; 

    string line; 
    while (getline(in, line)) 
    { 
     istringstream iss(line, istringstream::in); 

     if (!line.length()) 
      continue; 

     if (line[0] == '#') // Ignore the line starts with # 
      continue; 

     vector<string> words; 

     string word; 
     while (iss >> word) 
     { 
       words.push_back(word); 
     } 

     // now you have all words of current line 
     // you can use them to parse your file 
    } 

這是一個示例代碼,你應該#要跳過的空格。例如左修剪是有用的。

+0

什麼是矢量? – 2017-12-23 23:01:00

+0

@ e-info128:['std :: vector'](http://en.cppreference.com/w/cpp/container/vector) – deepmax 2017-12-24 08:03:43

2

中的東西的行:

#include <iostream> 
#include <string> 
#include <fstream> 
int main(){ 
    std::ifstream input("input"); 
    while (input.good()) { 
    char c = input.peek(); 
    if (c == '#' || c == '\n') { 
     input.ignore(256, '\n'); 
     continue; 
    } 
    std::string s1, s2; 
    int i; 
    input >> s1 >> s2 >> i; 
    if (input.good()) 
     std::cout << s1 << " - " << s2 << " - " << i << std::endl; 
    } 
    input.close(); 
    return 0; 
} 
0

或者改爲@ MM的回答,您可以使用新的<regex>能力的C++ 11.但是請注意,目前並非所有的標準庫實現這個完全。 ,所以如果有必要的話,你可能也想回到Boost.regex

#include <fstream> 
#include <iostream> 
#include <sstream> 

// note: Most C++11 regex implementations are not up to scratch, offer 
// Boost.regex as an alternative. 
#ifdef USE_BOOST_REGEX 
#include <boost/regex.hpp> 
namespace std 
{ 
    using ::boost::regex; 
    using ::boost::regex_match; 
    using ::boost::smatch; 
} 
#else 
#include <regex> 
#endif 

#include <string> 
#include <tuple> 
#include <vector> 

int main() 
{ 
    // open input file 
    std::ifstream in("file.txt"); 
    if (!in.is_open()) return 1; 
    // ECMAScript syntax! 
    std::regex empty_or_comment_re("\\s*(?:#.*)?"); 
    // note: only matches integers 
    std::regex values_re("\\s*(\\S+)\\s+(\\S+)\\s+(-?\\d+)\\s*"); 
    // will contain the results 
    std::vector<std::tuple<std::string, std::string, int> > results; 
    size_t lineno = 0; // for error reporting 
    std::string line; 
    // read lines 
    while (getline(in, line)) 
    { 
    ++lineno; 
    // match empty or comment lines 
    if (regex_match(line, empty_or_comment_re)) continue; 
    // match lines containing data 
    std::smatch match; 
    if (!regex_match(line, match, values_re)) 
    { 
     std::cerr<< "ERROR: malformed line in file.txt, line " << lineno 
     << ".\n"; 
     return 1; 
    } 
    // read integer from match 
    int n; 
    std::istringstream iss(match[3]); 
    iss >> n; 
    // append to results 
    results.push_back(std::make_tuple(match[1], match[2], n)); 
    } 
}