我正在研究一个项目,我需要读取一个CSV文件,然后用数据填充DataSet.我一直在寻找,我在OleDB中找到了一些有趣的东西.
我有一个类CSVReader:
class CSVReader
{
public DataTable GetDataTable(string filePath)
{
OleDbConnection conn = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0; Data Source = " + Path.GetDirectoryName(filePath) + "; Extended Properties = \"Text;HDR=YES;FMT=Delimited\"");
conn.Open();
string strQuery = "SELECT * FROM [" + Path.GetFileName(filePath) + "]";
OleDbDataAdapter adapter = new OleDbDataAdapter(strQuery, conn);
DataSet ds = new System.Data.DataSet("CSV File");
adapter.Fill(ds);
return ds.Tables[0];
}
}
Run Code Online (Sandbox Code Playgroud)
我从这里叫它:
CSVReader datareader = new CSVReader();
DataTable dt = datareader.GetDataTable(filepath);
Run Code Online (Sandbox Code Playgroud)
问题是它解析第一行(标题行)就像列的JUST ONE标识符一样,我的意思是:这是CSV文件的标题:
Name, Product Name, Server, Vendor, Start Time, End Time, Host Name, User …Run Code Online (Sandbox Code Playgroud) 我有这个:
typedef string domanin_name;
Run Code Online (Sandbox Code Playgroud)
然后,我尝试以这种方式重载运算符<
bool operator<(const domain_name & left, const domain_name & right){
int pos_label_left = left.find_last_of('.');
int pos_label_right = right.find_last_of('.');
string label_left = left.substr(pos_label_left);
string label_right = right.substr(pos_label_right);
int last_pos_label_left=0, last_pos_label_right=0;
while(pos_label_left!=string::npos && pos_label_right!=string::npos){
if(label_left<label_right) return true;
else if(label_left>label_right) return false;
else{
last_pos_label_left = pos_label_left;
last_pos_label_right = pos_label_right;
pos_label_left = left.find_last_of('.', last_pos_label_left);
pos_label_right = right.find_last_of('.', last_pos_label_left);
label_left = left.substr(pos_label_left, last_pos_label_left);
label_right = right.substr(pos_label_right, last_pos_label_right);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我知道这是一种使运算符重载的奇怪方法<,但我必须这样做.它应该做我想要的.这不是重点.
问题是它在这一行中进入无限循环:
if(label_left<label_right) return true;
Run Code Online (Sandbox Code Playgroud)
似乎它试图使用这个重载函数本身进行比较,但label_left是一个字符串,而不是域名!
有什么建议吗?