1 /***
2 * 解析csv文件
3 */
4 BOOL ParseCSVFile(string fileName)
5 {
6 //文件名错误
7 vector<string> fields; //声明一个字符串向量
8 string field;
9 SplitString(fileName.c_str,fields,".");
10 if (fields.size() < 2 || fields[fields.size()-1] != "csv")
11 {
12 //"文件格式错误";
13 }
14
15 ifstream fin(fileName); //打开文件流操作
16 string line;
17 int lineCount = 0;
18 while (getline(fin, line)) //整行读取,换行符“
”区分,遇到文件尾标志eof终止读取
19 {
20 vector<string> fields; //声明一个字符串向量
21 string field;
22 SplitString(line,fields,",");
23 if (fields.size() != 7)
24 {
25 continue;
26 }
27 string loginName = Trim(fields[0]); //用户登录名
28 string userName = Trim(fields[1]); //用户名称
29 string cardId = Trim(fields[2]); //身份证号
30 string sex = Trim(fields[3]); //性别
31 string ustatus = Trim(fields[4]); //状态
32 string invalidTime = TimeToDbTime(Trim(fields[5])); //到期时间
33 string department = Trim(fields[6]); //所属部分信息
34 if (lineCount == 0)
35 {
36 lineCount++;
37 continue;
38 }
39
40 lineCount++;
41
42 //具体处理方法。。。
43 }
44
45 return TRUE;
46 }
47
48 /***
49 * 按指定字符截取字符串
50 */
51 void SplitString(const string& str, vector<string>& ret_, const string &sep)
52 {
53 if (str.empty())
54 {
55 return ;
56 }
57
58 string tmp;
59 string::size_type pos_begin = 0;//str.find_first_not_of(sep);
60 string::size_type comma_pos = 0;
61
62 while (pos_begin != string::npos)
63 {
64 comma_pos = str.find(sep, pos_begin);
65 if (comma_pos != string::npos)
66 {
67 tmp = str.substr(pos_begin, comma_pos - pos_begin);
68 pos_begin = comma_pos + sep.length();
69 }
70 else
71 {
72 tmp = str.substr(pos_begin);
73 pos_begin = comma_pos;
74 }
75
76 ret_.push_back(tmp);
77 }
78 }
79
80 /***
81 * 删除字符串中空格,制表符tab等无效字符
82 */
83 string Trim(string& str)
84 {
85 str.erase(0,str.find_first_not_of("
"));
86 str.erase(str.find_last_not_of("
") + 1);
87 return str;
88 }
更多内容请访问 www.uusystem.com