MFC 进行CSV文件的读写
来源:互联网 发布:火凤凰软件下载 编辑:程序博客网 时间:2024/06/08 10:01
Csv.h
#pragma once
#include "afx.h"
class CCSVFile : public CStdioFile
{
public:
enum Mode { modeRead, modeWrite };
CCSVFile(LPCTSTR lpszFilename, Mode mode = modeRead);
~CCSVFile(void);
bool ReadData(CStringArray &arr);
void WriteData(CStringArray &arr);
CCSVFile GetCsvFileObject();
CCSVFile m_csvFile;
#ifdef _DEBUG
Mode m_nMode;
#endif
};
Csv.cpp
#include "StdAfx.h"
#include "Csv.h"
CCSVFile::CCSVFile(LPCTSTR lpszFilename, Mode mode)
: CStdioFile(lpszFilename, (mode == modeRead) ?
CFile::modeRead|CFile::shareDenyWrite|CFile::typeText
:
CFile::modeWrite|CFile::shareDenyWrite|CFile::modeCreate|CFile::typeText)
{
#ifdef _DEBUG
m_nMode = mode;
#endif
}
CCSVFile::~CCSVFile(void)
{
}
bool CCSVFile::ReadData(CStringArray &arr)
{
// Verify correct mode in debug build
ASSERT(m_nMode == modeRead);
// Read next line
CString sLine;
if (!ReadString(sLine))
return false;
LPCTSTR p = sLine;
int nValue = 0;
// Parse values in this line
while (*p != '\0')
{
CString s; // String to hold this value
if (*p == '"')
{
// Bump past opening quote
p++;
// Parse quoted value
while (*p != '\0')
{
// Test for quote character
if (*p == '"')
{
// Found one quote
p++;
// If pair of quotes, keep one
// Else interpret as end of value
if (*p != '"')
{
p++;
break;
}
}
// Add this character to value
s.AppendChar(*p++);
}
}
else
{
// Parse unquoted value
while (*p != '\0' && *p != ',')
{
s.AppendChar(*p++);
}
// Advance to next character (if not already end of string)
if (*p != '\0')
p++;
}
// Add this string to value array
if (nValue < arr.GetCount())
arr[nValue] = s;
else
arr.Add(s);
nValue++;
}
// Trim off any unused array values
if (arr.GetCount() > nValue)
arr.RemoveAt(nValue, arr.GetCount() - nValue);
// We return true if ReadString() succeeded--even if no values
return true;
}
void CCSVFile::WriteData(CStringArray &arr)
{
static TCHAR chQuote = '"';
static TCHAR chComma = ',';
// Verify correct mode in debug build
ASSERT(m_nMode == modeWrite);
// Loop through each string in array
for (int i = 0; i < arr.GetCount(); i++)
{
// Separate this value from previous
if (i > 0)
WriteString(_T(","));
// We need special handling if string contains
// comma or double quote
bool bComma = (arr[i].Find(chComma) != -1);
bool bQuote = (arr[i].Find(chQuote) != -1);
if (bComma || bQuote)
{
Write(&chQuote, sizeof(TCHAR));
if (bQuote)
{
for (int j = 0; j < arr[i].GetLength(); i++)
{
// Pairs of quotes interpreted as single quote
if (arr[i][j] == chQuote)
Write(&chQuote, sizeof(TCHAR));
TCHAR ch = arr[i][j];
Write(&ch, sizeof(TCHAR));
}
}
else
{
WriteString(arr[i]);
}
Write(&chQuote, sizeof(TCHAR));
}
else
{
WriteString(arr[i]);
}
}
WriteString(_T("\n"));
}
- MFC 进行CSV文件的读写
- CSV文件的读写
- VC对csv文件进行读写操作
- CSV 文件的读写练习
- CSV 文件的读写总结
- 读写csv文件的问题
- MFC文件的读写
- 用opencsv和用Bufferedreader/writer进行csv文件的读写及简单处理
- commons-csv读写CSV文件
- 对文件夹中所文件(csv)进行读写操作
- Python 标准库 csv —— csv 文件的读写
- 用C#写的读写CSV文件
- 一个读写csv文件的C#类
- 用C#写的读写CSV文件
- java读写Csv文件的方法
- 一个读写csv文件的C#类
- 利用javacsv实现CSV文件的读写
- OpenCV对csv文件的读写操作
- c# winform 关于 datagridview一些操作
- 第一章:【UCHome二次开发】uchome文件体系介绍
- Log4j.properties 属性详解
- PHP $_SERVER详细参数
- ORA-00214 控制文件版本不一致恢复过程
- MFC 进行CSV文件的读写
- photoshop脚本:批量更改图片尺寸的脚本
- 黑马程序员_学习日记七_集合二
- MFC 文件浏览,判断是否存在
- poj 2179
- VS2010 C#调用C++ DLL文件
- No pains no gains
- POJ1426
- Fragment例子 .