zoukankan      html  css  js  c++  java
  • c++解析csv文件

     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

  • 相关阅读:
    链表--判断一个链表是否为回文结构
    矩阵--“之”字形打印矩阵
    二叉树——平衡二叉树,二叉搜索树,完全二叉树
    链表--反转单向和双向链表
    codeforces 490C. Hacking Cypher 解题报告
    codeforces 490B.Queue 解题报告
    BestCoder19 1001.Alexandra and Prime Numbers(hdu 5108) 解题报告
    codeforces 488A. Giga Tower 解题报告
    codeforces 489C.Given Length and Sum of Digits... 解题报告
    codeforces 489B. BerSU Ball 解题报告
  • 原文地址:https://www.cnblogs.com/tianjifa/p/10443113.html
Copyright © 2011-2022 走看看