保存表头,通过序列号找到相应数据,通过表头获取对应的值
#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;
}