C++ split函数
#include "vector"
#include <string>
#include <iostream>
void split(std::string str,std::string pattern, std::vector<std::string> &result)
{
std::string::size_type pos;
std::string strTemp(str + pattern);
size_t size = strTemp.size();
for(int i = 0; i < size; i++)
{
pos = strTemp.find(pattern, i);
if(pos < size && i < pos)
{
std::string s = strTemp.substr(i,pos-i);
result.push_back(s);
i = (int)(pos+pattern.size()-1);
}
}
}
typedef std::vector<std::vector<std::string>> VEC_VEC_STR;
bool getLineToVector(const char *pszFileName, VEC_VEC_STR &vecLines)
{
FILE *pFile = NULL;
char szLine[1024];
if((pFile = fopen(pszFileName,"r")) == NULL)
{
std::cout << __FUNCTION__ << "::open file error!" << std::endl;
return false;
}
std::string strLine = "";
while (!feof(pFile))
{
memset(szLine, 0, 1024);
fgets(szLine,1024,pFile);
while (szLine[strlen(szLine) - 1] == '\n'
|| szLine[strlen(szLine) - 1] == '\r') {
szLine[strlen(szLine) - 1] = '\0';
}
strLine = szLine;
if (std::string::npos != strLine.find("#")) {
//log
continue;
}
//Dealsub
// std::vector<std::string> vecLine;
// split(strLine, ",", vecLine);
// if (vecLine.size() != 3)
// {
// //log
// continue;
// }
// else {
// vecLines.push_back(vecLine);
// }
}
fclose(pFile);
return true;
}