2016-02-19 73 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; 
}