2016-02-19 103 views
1

檢查.csv文件中的逗號時出現問題。對於每一行,我希望在找到第10個逗號時執行操作,並檢查逗號後面的值。此值始終是小於9的數字。在字符串C++(無子字符串)上使用str.find()時執行操作

int main() 
{ 
    string row; 
    ifstream infile; 
    infil.open ("file.csv"); 
    int sum = 0; 

    while(getline(infil,row)) 
    { 
     for(int i = 0; i < row.size() ;i++) 
     { 
      if(row.find(',') != std::string::npos) 
      { 
       sum++; 
      } 
      if(sum == 10) 
      { 
       //PERFORM OPERATION 
      } 
     } 
    } 
return 0; 
} 

我寫的代碼不起作用,有幫助嗎?

+1

不工作*如何*? – Biffen

+2

將'int sum = 0;'放在while循環中。 –

+3

......也許是因爲你只是反覆調用'find()',每次只能找到相同的逗號(第一個)。 *而且*你永遠不會在任何地方保存找到的位置。 – Biffen

回答

0

嘗試類似的東西

int main() 
{ 
    string row; 
    ifstream infile; 
    infile.open ("file.csv"); 
    int sum = 0; 

    while(getline(infile,row)) 
    { 
     // sum = 0; // you can reset sum at start of loop if you want, 
        // or continue to use sum from last iteration 
     int pos = row.find(','); // find first comma 
     while (pos != std::string::npos) 
     { 
      if (++sum == 10) 
      { 
       //PERFORM OPERATION 
       sum = 0; // reset comma count if you want 
      } 
      pos = row.find(',', ++pos); // find next comma (after pos) 
     } 
    } 
    return 0; 
} 
+1

我認爲你錯了。你需要重置每行的「總和」。 *「當找到第10個逗號時,每一行都是」* –

+0

@Simon Kraemer看看評論。 這取決於我們如何計算逗號。每行或不行 –

1

您可以使用這樣的事情:

#include <iostream> 
#include <fstream> 
#include <string> 

using std::string; 
using std::ifstream; 
using std::cout; 
using std::cerr; 
using std::endl; 

int main() 
{ 
    ifstream infile; 
    //infile.open("file.csv"); 
    infile.open("C:\\Users\\Kraemer\\Desktop\\test.csv"); 

    string row; 
    while (getline(infile, row)) 
    { 
     int sum = 0; //number of commas 
     size_t pos = 0; //Position in row 

     //As long as we didn't find 10 commas AND there is another comma in this line 
     while(sum < 10 && (pos = row.find(',', pos)) != string::npos) 
     { 
      //Comma found 
      sum++; 
      //Set position behind the comma 
      pos++; 
     } 

     //When we come here sum is always <= 10 

     if(sum == 10) 
     { //10 commas found 
      cerr << "Found 10 commas" << endl; 
     } 
     else 
     { 
      cerr << "Did not find enough commas in line" << endl; 
     } 
    } 
    return 0; 
} 

你也應該注意到,getline(infile, row)也將在EOF是在包含數據的最後一行失敗。 因此,您需要在infile.eof()返回true時檢查最後一個讀取行,或者確保輸入數據以空行結束。

要提取的數字第十逗號後,你可以做這樣的事情:

if (sum == 10) 
{ //10 commas found 
    cerr << "Found 10 commas" << endl; 

    if (pos < row.size()) 
    { 
     char digitAsChar = row[pos]; 
     if (digitAsChar >= '0' && digitAsChar <= '9') // Valid digit 
     { 
      int digitAsInt = digitAsChar - '0'; //Convert from char to int 
      cout << "Found digit " << digitAsInt << endl; 
     } 
     else 
     { 
      cerr << "Character '" << digitAsChar << "' is no digit." << endl; 
     } 
    } 
    else 
    { 
     cerr << "10th comma is at the end of the line - no digit found" << endl; 
    } 
} 
else 
{ 
    cerr << "Did not find enough commas in line" << endl; 
} 

輸入:

,,,,,,,,,,1 
,,,,,,,,,,2 
,,,,,,,,,,3 
,,,,,,,,,,4 
,,,,,,,,,,5 
,,,,,,,,,,f 
,,,,,,,,,, 
,,,,,,,,,,8 
,,,,,,,,,9 
,,,,,,,10 

輸出:

Found 10 commas 
Found digit 1 
Found 10 commas 
Found digit 2 
Found 10 commas 
Found digit 3 
Found 10 commas 
Found digit 4 
Found 10 commas 
Found digit 5 
Found 10 commas 
Character 'f' is no digit. 
Found 10 commas 
10th comma is at the end of the line - no digit found 
Found 10 commas 
Found digit 8 
Did not find enough commas in line 
Did not find enough commas in line 
+0

@Biffen:更新我的答案 - 謝謝你指出這一點。 –

0

這裏是一個有點最小的實現(不包括從文件)讀:

#include <iostream> 
#include <string> 

int main() { 
    const std::string  row = "a,b,c,d,e,f,g,h,i,j,7,k,l"; 

    size_t     commas = 0; 
    std::string::size_type pos = 0; 

    while (++commas <= 10) { 
    pos = row.find(',', pos); 
    if (pos == std::string::npos) { 
     // Out of commas 
     break; 
    } else { 
     ++pos; 
    } 
    } 

    if (pos != std::string::npos) { 
    int number = std::stoi(row.substr(pos, 1)); 
    std::clog << "number = " << number << std::endl; 
    } 
} 

輸出:

number = 7 

關鍵是要保持最後發現逗號的位置(這開始爲0之前任何逗號已找到)的軌道並將其用於std::string::find()的第二個參數。

擴大到一個「真實」的實施應該包裹std::stoi()中的呼叫try並在catch做任何需要做,如果一切以之後的第十個逗號不是數字(或過大)。在row(或第十個逗號是最後一個字符)中沒有十個逗號的情況下,可以在最後一個塊之後放置一個else並執行任何需要的操作。

+0

它僅適用於10個逗號 –

+0

當pos將是string :: npos時,++ pos將返回(npos + 1),即0. 和(++ pos!= std :: string :: npos)將返回true –

+0

如果小於10個逗號,您的代碼將無法正常工作。 如果行不包含任何逗號,則代碼將在無限循環中掛起 –

0

不使用字符串::找到一個簡單的解決辦法是這樣的:

int main() { 
    string s = "1,2,3,4,5,6,7,8,9,10,11"; 
    int n = 0; 
    int i = 0; 
    for(auto c : s) 
    { 
     if (c == ',') 
     { 
      ++n; 
      if (n == 10) break; 
     } 
     ++i; 
    } 

    if (n == 10) 
    { 
     string tmp = s.substr(i+1); 
     cout << "Rest of string is: " << tmp << endl; 
    } 
    else 
    { 
     cout << "Only found " << n << endl; 
    } 
    return 0; 
}