文本操作和读写文件

对字符串根据字符进行分段

1
2
3
4
5
6
7
8
9
vector<std::string> params;
std::string file = "123.45.abc.0";
std::istringstream param_stream(file);
std::string param;
while(getline(param_stream, param, '.'))
{
cout << param <<endl;
params.push_back(param);
}

运行结果:

1
2
3
4
123
45
abc
0

读Excel文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
bool readCSVInput(int N, vector<double> &input_points_x, vector<double> &input_points_y, vector<double> &anchor_points_x, vector<double> &anchor_points_y)
{
ifstream readCSV("/home/user/data.csv", ios::in);
int lineNum = 0;
string lineString;
vector<vector<string>> stringArray;
cout << endl;
while (getline(readCSV, lineString) )
{
//cout << lineString << endl;
if (lineNum > 0)
{
stringstream ss(lineString);
string s;
vector<string> oneLineStrings;
while (getline(ss, s, ',') )
oneLineStrings.push_back(s);
if (oneLineStrings.size() != 5)
{
cout << "ERROR:oneLineStrings.size() != 5" << endl;
return false;
}
else
{
input_points_x.push_back(std::stod(oneLineStrings[1]));
input_points_y.push_back(std::stod(oneLineStrings[2]));
anchor_points_x.push_back(std::stod(oneLineStrings[3]));
anchor_points_y.push_back(std::stod(oneLineStrings[4]));
}
}
lineNum++;
}
if (N == input_points_x.size())
{
return true;
}
else
{
input_points_x.clear();
input_points_y.clear();
anchor_points_x.clear();
anchor_points_y.clear();
cout << "ERROR:N == input_points_x.size()" << endl;
return false;
}
}