資源簡介
VC++6.0編譯通過的讀寫CSV文件的代碼及實例
在VC++6.0中編譯通過,實測可用
通用性很不錯
包含CSV讀取的實例

代碼片段和文件信息
#include?“CSVOperator.h“
//////////////////////////////////////////////////////////////////////////
//CSV?operator
//?Download?by?http://www.codefans.net
CCSVOperator::CCSVOperator(const?char*?path)
{
????LoadCSV(path);
}
bool?CCSVOperator::LoadCSV(const?char*?path)
{
????FILE*?pfile?=?fopen(path?“r“);
????if?(pfile)
????{
????????fseek(pfile0SEEK_END);
????????u32?dwsize?=?ftell(pfile);
????????rewind(pfile);
????????char*?filebuffer?=?new?char[dwsize];
????????fread(filebuffer?1?dwsize?pfile);
????????std::map?StringMap;
????????char*?pBegin?=?filebuffer;
????????char*?pEnd?=?strchr(filebuffer?‘\n‘);
????????u32?uiIndex?=?1;
????????while?(pEnd?!=?NULL)
????????{
????????????std::string?strbuff;
????????????strbuff.insert(0?pBegin?pEnd-pBegin);
????????????if?(!strbuff.empty())
????????????{
????????????????StringMap[uiIndex]?=?strbuff;
????????????}
????????????pBegin?=?pEnd?+?1;
????????????pEnd?=?strchr(pEnd?+?1?‘\n‘);
????????????++uiIndex;
????????}
????????delete[]?filebuffer;
????????std::map::iterator?iter?=?StringMap.begin();
????????for?(;?iter?!=?StringMap.end();?++iter)
????????{
????????????std::vector?StringVec;
????????????std::map?l_StringMap;
????????????StringParser::GetParamFromString(iter->second?StringVec);
????????????for?(int?i?=?0;?i?????????????{
????????????????l_StringMap[i+1]?=?StringVec.at(i);
????????????}
????????????m_StringKeyMap[iter->first]?=?l_StringMap;
????????}
????????fclose(pfile);
????????m_CSVName?=?path;
????????return?true;
????}
????return?false;
}
bool?CCSVOperator::GetInt(u32?uiLine?u32?uiRow?int&?iValue)
{
????std::string*?pKey?=?GetString(uiLine?uiRow);
????if?(pKey)
????{
????????iValue?=?atoi(pKey->c_str());
????????return?true;
????}
????else
????{
????????return?false;
????}
}
bool?CCSVOperator::GetFloat(u32?uiLine?u32?uiRow?float&?fValue)
{
????std::string*?pKey?=?GetString(uiLine?uiRow);
????if?(pKey)
????{
????????fValue?=?atof(pKey->c_str());
????????return?true;
????}
????else
????{
????????return?false;
????}
}
std::string*?CCSVOperator::GetString(u32?uiLine?u32?uiRow)
{
????std::map?>::iterator?iterLine?=?m_StringKeyMap.find(uiLine);
????if?(iterLine?!=?m_StringKeyMap.end())
????{
????????std::map&?rStringMap?=?iterLine->second;
????????std::map::iterator?iterRow?=?rStringMap.find(uiRow);
????????if?(iterRow?!=?rStringMap.end())
????????{
????????????return?&iterRow->second;
????????}
????????else
????????{
????????????return?NULL;
????????}
????}
????else
????{
????????return?NULL;
????}
}
bool?CCSVOperator::SetNumber(u32?uiLine?u32?uiRow?int?iValue)
{
????std::string*?pKey?=?GetString(uiLine?uiRow);
????if?(pKey)
????{
????????char?buffer[100];
????????memset(buffer?0?sizeof(
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2017-09-08?10:03??csvdx\
?????文件????????4708??2017-09-08?09:43??csvdx\CSVOperator.cpp
?????文件?????????789??2009-07-11?02:14??csvdx\CSVOperator.h
?????文件?????????808??2017-09-08?09:47??csvdx\CSV_OP.cpp
?????目錄???????????0??2017-09-08?09:47??csvdx\Debug\
?????文件??????335708??2017-09-08?09:43??csvdx\Debug\CSVOperator.obj
?????文件??????298462??2017-09-08?09:47??csvdx\Debug\CSV_OP.obj
?????文件??????614456??2017-09-08?09:47??csvdx\Debug\csvdx.exe
?????文件??????931872??2017-09-08?09:47??csvdx\Debug\csvdx.ilk
?????文件?????7564852??2017-09-08?09:30??csvdx\Debug\csvdx.pch
?????文件?????1295360??2017-09-08?09:47??csvdx\Debug\csvdx.pdb
?????文件??????222208??2017-09-08?09:53??csvdx\Debug\vc60.idb
?????文件??????200704??2017-09-08?09:47??csvdx\Debug\vc60.pdb
?????文件????????2484??2009-07-14?22:56??csvdx\StringParser.h
?????文件?????????125??2009-07-14?23:20??csvdx\config.csv
?????文件????????4674??2017-09-08?09:33??csvdx\csvdx.dsp
?????文件?????????518??2017-09-08?09:29??csvdx\csvdx.dsw
?????文件???????50176??2017-09-08?10:03??csvdx\csvdx.ncb
?????文件???????48640??2017-09-08?10:03??csvdx\csvdx.opt
?????文件??????119084??2017-09-08?09:47??csvdx\csvdx.plg
評論
共有 條評論