MFC中如何读写CSV文件的数据
我现在有很多数据,想通过CSV文件来读写,在网上下载一个类,#pragmaonce#include"StringParser.h"classCCSVOperator{pu...
我现在有很多数据,想通过CSV文件来读写,在网上下载一个类,
#pragma once
#include "StringParser.h"
class CCSVOperator
{
public:
CCSVOperator(){};
~CCSVOperator(){};
CCSVOperator(const char* path);
bool LoadCSV(const char* path);
bool SaveCSV(const char* path = NULL);
bool GetInt(u32 uiLine, u32 uiRow, int& iValue);
bool GetFloat(u32 uiLine, u32 uiRow, float& fValue);
std::string* GetString(u32 uiLine, u32 uiRow);
bool SetNumber(u32 uiLine, u32 uiRow, int iValue);
bool SetNumber(u32 uiLine, u32 uiRow, float fValue);
bool SetString(u32 uiLine, u32 uiRow, const char* pStr);
std::map<u32, std::map<u32, std::string> >& GetCSVMap(){return m_StringKeyMap;}
protected:
std::string m_CSVName;
std::map<u32, std::map<u32, std::string> > m_StringKeyMap;
};
不知道有没有人用过,我现在用它里面SaveCSV,不能写入,连LoadCSV后都不能在里面写,写是用SetNUmber吗?
有没有人可以提供一份可以读写的类? 展开
#pragma once
#include "StringParser.h"
class CCSVOperator
{
public:
CCSVOperator(){};
~CCSVOperator(){};
CCSVOperator(const char* path);
bool LoadCSV(const char* path);
bool SaveCSV(const char* path = NULL);
bool GetInt(u32 uiLine, u32 uiRow, int& iValue);
bool GetFloat(u32 uiLine, u32 uiRow, float& fValue);
std::string* GetString(u32 uiLine, u32 uiRow);
bool SetNumber(u32 uiLine, u32 uiRow, int iValue);
bool SetNumber(u32 uiLine, u32 uiRow, float fValue);
bool SetString(u32 uiLine, u32 uiRow, const char* pStr);
std::map<u32, std::map<u32, std::string> >& GetCSVMap(){return m_StringKeyMap;}
protected:
std::string m_CSVName;
std::map<u32, std::map<u32, std::string> > m_StringKeyMap;
};
不知道有没有人用过,我现在用它里面SaveCSV,不能写入,连LoadCSV后都不能在里面写,写是用SetNUmber吗?
有没有人可以提供一份可以读写的类? 展开
2个回答
展开全部
csv文件格式比较简单,就是用逗号分割数据,我原来做过一个,自己编的读写函数,没有用专用的类:代码如下:
void CRespondDlg::OnButtonSave()
{
// szFilters is a text string that includes two file name filters:
// "*.my" for "MyType Files" and "*.*' for "All Files."
char szFilters[]=
"数据文件 (*.csv)|*.csv|All Files (*.*)|*.*||";
// Create an Open dialog; the default file name extension is ".my".
CFileDialog fileDlg (FALSE, "csv", "*.csv",
OFN_CREATEPROMPT|OFN_OVERWRITEPROMPT , szFilters, this);
CString msg;
CString tmsg,smsg;
tmsg.Format("");
smsg.Format("-");
// Display the file dialog. When use
if( fileDlg.DoModal ()==IDOK )
{
CString pathName = fileDlg.GetPathName();
// Implement opening and reading file in here.
CString message;
RsltFile.open(pathName,ios::out);
OnButtonTransform();
//Save Result
msg.Format("版本:,1.00\n");
RsltFile<<msg;
msg.Format("名称:,");
RsltFile<<msg;
………….
RsltFile.close();
}
}
void CRespondDlg::OnButtonOpen()
{
// szFilters is a text string that includes two file name filters:
// "*.my" for "MyType Files" and "*.*' for "All Files."
char szFilters[]=
"数据文件 (*.csv)|*.csv|All Files (*.*)|*.*||";
int i,j;
double nversion;
CString msg;
int ntemp;
CString tmppathname;
ifstream tmpinfile;
ofstream tmpoutfile;
char temp;
tmppathname.Format("temp000001");
// Create an Open dialog; the default file name extension is ".my".
CFileDialog fileDlg (TRUE, "csv", "*.csv",
OFN_FILEMUSTEXIST| OFN_HIDEREADONLY, szFilters, this);
// Display the file dialog. When use
if( fileDlg.DoModal ()==IDOK )
{
CString pathName = fileDlg.GetPathName();
// Implement opening and reading file in here.
CString message;
CString version1;
CString cversion;
CString msg;
double tmp;
CString tmp1,tmp2,tmp3;
ParFile.open(pathName,ios::nocreate);
char mark[100];
//Get Pile Parameter
//ParFile>>mark;
//msg.Format("%s",mark);
//打开暂存文件
tmpoutfile.open(tmppathname,ios::out);
//读入tmp文件进行格式转化:逗号“,”转为TAB并存入暂存文件
while(!ParFile.eof())
{
ParFile.get(temp);
if(temp==',')
temp='\t';
if(!ParFile.eof())
tmpoutfile.put(temp);
}
ParFile.close();
tmpoutfile.close();
//由暂存文件读入数据
tmpinfile.open(tmppathname,ios::nocreate);
tmpinfile>>mark; //版本号提示
tmpinfile>>nversion; //版本号
if(nversion==1)//1.00版本
{
//MessageBox("OK");
tmpinfile>>mark; tmpinfile>>mark; m_project.Format("%s",mark);//名称
tmpinfile>>mark; tmpinfile>>mark; m_tester.Format("%s",mark);//单位
}
UpdateData(false);
//OnButtonTransform();
UpdateData(false);
}
void CRespondDlg::OnButtonSave()
{
// szFilters is a text string that includes two file name filters:
// "*.my" for "MyType Files" and "*.*' for "All Files."
char szFilters[]=
"数据文件 (*.csv)|*.csv|All Files (*.*)|*.*||";
// Create an Open dialog; the default file name extension is ".my".
CFileDialog fileDlg (FALSE, "csv", "*.csv",
OFN_CREATEPROMPT|OFN_OVERWRITEPROMPT , szFilters, this);
CString msg;
CString tmsg,smsg;
tmsg.Format("");
smsg.Format("-");
// Display the file dialog. When use
if( fileDlg.DoModal ()==IDOK )
{
CString pathName = fileDlg.GetPathName();
// Implement opening and reading file in here.
CString message;
RsltFile.open(pathName,ios::out);
OnButtonTransform();
//Save Result
msg.Format("版本:,1.00\n");
RsltFile<<msg;
msg.Format("名称:,");
RsltFile<<msg;
………….
RsltFile.close();
}
}
void CRespondDlg::OnButtonOpen()
{
// szFilters is a text string that includes two file name filters:
// "*.my" for "MyType Files" and "*.*' for "All Files."
char szFilters[]=
"数据文件 (*.csv)|*.csv|All Files (*.*)|*.*||";
int i,j;
double nversion;
CString msg;
int ntemp;
CString tmppathname;
ifstream tmpinfile;
ofstream tmpoutfile;
char temp;
tmppathname.Format("temp000001");
// Create an Open dialog; the default file name extension is ".my".
CFileDialog fileDlg (TRUE, "csv", "*.csv",
OFN_FILEMUSTEXIST| OFN_HIDEREADONLY, szFilters, this);
// Display the file dialog. When use
if( fileDlg.DoModal ()==IDOK )
{
CString pathName = fileDlg.GetPathName();
// Implement opening and reading file in here.
CString message;
CString version1;
CString cversion;
CString msg;
double tmp;
CString tmp1,tmp2,tmp3;
ParFile.open(pathName,ios::nocreate);
char mark[100];
//Get Pile Parameter
//ParFile>>mark;
//msg.Format("%s",mark);
//打开暂存文件
tmpoutfile.open(tmppathname,ios::out);
//读入tmp文件进行格式转化:逗号“,”转为TAB并存入暂存文件
while(!ParFile.eof())
{
ParFile.get(temp);
if(temp==',')
temp='\t';
if(!ParFile.eof())
tmpoutfile.put(temp);
}
ParFile.close();
tmpoutfile.close();
//由暂存文件读入数据
tmpinfile.open(tmppathname,ios::nocreate);
tmpinfile>>mark; //版本号提示
tmpinfile>>nversion; //版本号
if(nversion==1)//1.00版本
{
//MessageBox("OK");
tmpinfile>>mark; tmpinfile>>mark; m_project.Format("%s",mark);//名称
tmpinfile>>mark; tmpinfile>>mark; m_tester.Format("%s",mark);//单位
}
UpdateData(false);
//OnButtonTransform();
UpdateData(false);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询