检查.csv文件中的逗号时遇到问题。对于每行,我想在找到第10个逗号时执行操作,并检查该逗号后的值。该值始终是小于9的数字。
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;
}
我写的代码没有用,有什么帮助吗?
答案 0 :(得分: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
答案 1 :(得分: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;
}
答案 2 :(得分: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
执行任何需要做的事情,如果在第十个逗号之后发生的任何事情都不是&#39 ; ta数(或太大)。并且可能在最后一个块之后添加else
并在row
中没有十个逗号(或者第十个逗号是最后一个字符)时执行任何需要做的事情。
答案 3 :(得分:0)
不使用string :: find的简单解决方案可能是这样的:
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;
}