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"));
}

原创粉丝点击