C++读取csv文件

来源:互联网 发布:win7安装数据库2000 编辑:程序博客网 时间:2024/04/29 18:36

保存表头,通过序列号找到相应数据,通过表头获取对应的值

#include "stdafx.h"#include <fstream>#include <sstream>#include <map>#include <Windows.h>using namespace std;int _tmain(int argc, _TCHAR* argv[]){ifstream file( "D:/test.csv" );string line;map< string, map< string, string > > m_Tables;map< int, string > m_Head;bool bHead = true;while ( getline( file, line ) ){istringstream sin(line);map <string, string> fields;string field;int count = 0;bool bDoneHead = false;while ( getline( sin, field, '\t' ) ){if ( !bHead ){fields[ m_Head[ count ] ] = field;} else{//表头m_Head[ count ] = field;bDoneHead = true;}count++;}if ( bDoneHead ){//已经读过表头了bHead = false;}else{for ( map <string, string>::iterator iter = fields.begin(); iter != fields.end(); iter++ ){if ( iter->first == m_Head[ 0 ] ){m_Tables[ iter->second ] = fields;break;}}}} system("pause");return 0;}


0 0
原创粉丝点击