-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_commen_func.cpp
72 lines (66 loc) · 1.9 KB
/
my_commen_func.cpp
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//读取文件路径下全部文件名称,并保存在vector中
#include "dirent.h"
#include <iostream>
#include <vector>
#include <sstream>
#include <string.h>
#include <fstream>
using namespace std;
/*
@para: file_list: 用来保存文件名称的vector
folder_path: 文件夹的路径
@return: None
*/
void getFileList(std::vector<std::string> &file_list, std::string folder_path,
bool concate = true, bool verbose = false) {
dirent *ptr;
DIR *dir;
file_list.clear();
dir = opendir(folder_path.c_str());
std::size_t pos;
folder_path.find_last_of("/", pos);
if (pos != (folder_path.size() - 1)) folder_path.push_back('/');
while ((ptr = readdir(dir)) != NULL) {
if (strcmp(ptr->d_name, ".") == 0 || strcmp(ptr->d_name, "..") == 0)
continue;
std::stringstream ss;
if (concate)
ss << folder_path << ptr->d_name;
else {
ss << ptr->d_name;
}
file_list.emplace_back(ss.str());
if (verbose) {
std::cout << ss.str() << std::endl;
}
}
closedir(dir);
}
/**
读取txt文件夹中的数据,一行一行的读取
*/
void SplitString(std::string str, std::vector<std::string> &fields,
char separators, bool verbose = false) {
std::istringstream sin(str);
std::string field;
while (std::getline(sin, field, separators)) {
fields.push_back(field);
if (verbose) {
std::cout << field << "--";
}
}
if (verbose) std::cout << std::endl;
}
void Readtxt(std::string file_path,std::vector<std::string> &fields,char separator) {
std::fstream fin;
fin.open(file_path.c_str(),std::ios::out);
if(!fin.is_open())
{
ROS_ERROR("can't open file %s",file_path.c_str());
return;
}
std::string line;
while (std::getline(fin, line)) {
SplitString(line, fields, separator);//将line一整句string按照分号切割成多个string并存储到fields中vector中;
}
}