2014-09-22 35 views
0

我想解析文本文件中的行。每一行代表一個股票代碼並具有相同的格式,有隨機數字的字符串(公司名稱+符號),然後是統一數量的浮動(每行的金額相同)。解析一個文本文件,直到找到一個浮點數?

如何檢查最後一次讀取的字符串是否爲浮點數,以瞭解何時到達字符串末尾並開始解析浮點數?文字

採樣線:

A.M. CASTLE & COMPANY CAS 15.71 0.55 3.63 31.57 17.97 8.99 7.79 
AAR CORP AIR 17.79 0.19 1.08 30.62 18.45 10.51 38.26 
ABBOTT LABORATORIES ABT 45.14 0.01 0.02 -3.24 50.00 40.25 20.33 

示例代碼:

void parse(string filename){ 
    ifstream myfile;   
    string line; 
    string current_word; 

    myfile.open(filename); 
    if (myfile.is_open()){ 
     while (getline(myfile, line)) 
     { 
      stringstream current_line(line); 
      while (current_line >> current_word){ 
       // How can I test when I have reached a float here? 
      } 
     } 
    } 
} 
+2

你應該在你的問題中包括一個[最小,完整和可驗證的例子](http://stackoverflow.com/help/mcve)當前嘗試,所以我們更好地瞭解你在做什麼。這將幫助我們提供一個合適的答案。 – 2014-09-22 02:17:08

+0

你保證在第一個浮點數之前沒有數字出現嗎? – 2014-09-22 02:17:38

+0

是的,只有字母或標點符號(連字符,圓括號,&符號等)在字符串到達​​之前。 – CGutz 2014-09-22 02:22:38

回答

2

可以測試數量的流閱讀,看它是否成功,並使用值,如果它確實是這樣的:

int main() 
{ 
    std::string line = "A.M. CASTLE & COMPANY CAS 15.71 0.55 3.63 31.57 17.97 8.99 7.79"; 

    std::istringstream iss(line); // convert the line into a stream 

    std::string item; 
    while(iss >> item) // read the stream items (space separated) one by one 
    { 
     float f; 
     if(std::istringstream(item) >> f) // does this item read as a float? 
     { 
      // use f here if it does 
      std::cout << f << " "; 
     } 
    } 
} 
1

如果你保證,沒有任何浮動前的字符串將包含一個數字,那麼你可以簡單地比較每一個字符直到找到一個數字,並且找到了浮點數的第一個字符。

沒有這個保證,我可能會解析單詞。將每個字符添加到字符串直到找到空白爲止是一件小事。如果該字符串只包含數字和一個句點,那麼你已經找到了你的浮點數。否則,跳過下一個非空白字符,並再次執行相同的操作。

0

您可以使用sscanf(),這將做一個行所請求的操作。

bool parseNameAndFloats(char const *input, char *name, unsigned int namesize, float *floatArray, unsigned int floatsize) 
{ 
    // Assuming the sample string is representative, there's 7 floats in it. 
    if (floatsize < 7) 
    { 
     return false; 
    } 
    char *temp = strdup(input); 
    if (temp == NULL) 
    { 
     // deal with allocation failure in strdup; 
     return false; 
    } 
    int count = sscanf(input, "%[^0-9.]s %f %f %f %f %f %f %f", temp, floatArray, floatArray + 1, floatArray + 2, floatArray + 3, floatArray + 4,floatArray + 5, floatArray + 6); 
    if (namesize > 0) 
    { 
     strncpy(name, temp, namesize); 
     name[namesize - 1] = 0; 
    } 
    free(temp); 
    return count == 8; 
} 

還有那些會批評sscanf(),並錯誤地使用時,可能會導致一些重大問題。其中一個原因是我使用strdup()來複制原始輸入字符串。這保證讓我有足夠的緩衝區來保存%[^0-9.]s轉換的結果。然後我使用strncpy()來提取不超過將提供的緩衝區,並確保NUL終止。

0

使用C++ 11中提供的正則表達式。

仔細檢查浮點數的模式。例如。我的表情不允許領先的跡象。

#include <iostream> 
#include <ostream> 
#include <regex> 
#include <sstream> 
#include <string> 

int main() 
{ 
    std::istringstream input("A.M. CASTLE & COMPANY CAS 15.71 0.55 3.63 31.57 17.97 8.99 7.79"); 

    // Pattern for recognizing floating-point numbers 
    std::regex pattern(R"(\d+\.(\d*)?((e|E)(\+|\-)?\d+)?)"); 

    for (std::string line; std::getline(input, line);) 
    { 
     // We have a successful read of one line 
     // Now extract the floating-point numbers on that line 

     auto first = std::sregex_iterator(line.cbegin(), line.cend(), pattern); 
     auto last = std::sregex_iterator(); 
     for (; first != last; ++first) 
     { 
      double d = std::stof(first->str()); 
      std::cout << d << std::endl; 
     } 
    } 

    return 0; 
} 
相關問題