Chinaunix首页 | 论坛 | 博客
  • 博客访问: 21246
  • 博文数量: 9
  • 博客积分: 25
  • 博客等级: 民兵
  • 技术积分: 57
  • 用 户 组: 普通用户
  • 注册时间: 2012-07-27 10:58
文章分类

全部博文(9)

文章存档

2014年(6)

2013年(3)

我的朋友

分类: C/C++

2014-05-08 01:13:35


  1. #include <cstdio>
  2. #include <iostream>
  3. #include <cstring>
  4. #include <cstdlib>

  5. using namespace std;

  6. class CFileUnit
  7. {
  8.     public:
  9.         CFileUnit();
  10.         ~CFileUnit();
  11.         void GetFilePath(const char* psrFilePath);
  12.         void OpenFile();
  13.         bool isOpen();
  14.         void GetSQLStatement(char* psrStmt);
  15.         static void Erase(char* psrStmt);
  16.         bool isEndFile();
  17.     private:
  18.         int m_iPos;
  19.         FILE* m_pfFile;
  20.         char m_srFilePath[128];
  21.         bool m_bEndFile;
  22. };

  23. CFileUnit::CFileUnit()
  24. {
  25.     m_iPos = 0;
  26.     m_pfFile = NULL;
  27.     m_bEndFile = false;
  28.     memset(m_srFilePath, 0, sizeof(m_srFilePath));
  29. }

  30. CFileUnit::~CFileUnit()
  31. {
  32.     if (!isOpen())
  33.     {
  34.         fclose(m_pfFile);
  35.     }
  36. }

  37. bool CFileUnit::isEndFile()
  38. {
  39.     if (m_bEndFile)
  40.     {
  41.         return true;
  42.     }
  43.     return false;
  44. }

  45. bool CFileUnit::isOpen()
  46. {
  47.     if (m_pfFile != NULL)
  48.     {
  49.         return false;
  50.     }
  51.     else
  52.     {
  53.         return true;
  54.     }
  55. }

  56. void CFileUnit::GetFilePath(const char* psrFilePath)
  57. {
  58.     if (NULL == psrFilePath)
  59.     {
  60.         return;
  61.     }
  62.     memset(m_srFilePath, 0, sizeof(m_srFilePath));
  63.     strncpy(m_srFilePath, psrFilePath, sizeof(m_srFilePath));
  64. }

  65. void CFileUnit::OpenFile()
  66. {
  67.     m_pfFile = fopen(m_srFilePath, "r");
  68.     if (NULL == m_pfFile)
  69.     {
  70.     }
  71. }

  72. void CFileUnit::GetSQLStatement(char* psrStmt)
  73. {
  74.     if (NULL == psrStmt)
  75.     {
  76.     }

  77.     char chTmp = 0;
  78.     char chPrev = 0;
  79.     int iLen = 0;
  80.     bool bInString = false;
  81.     char srTmp[128] = {'\0'};

  82.     while ((chTmp = fgetc(m_pfFile)) != EOF)
  83.     {
  84.         if ('\'' == chTmp)
  85.         {
  86.             if (chPrev != '\\')
  87.             {
  88.                 if (!bInString)
  89.                 {
  90.                     bInString = true;
  91.                 }
  92.                 else
  93.                 {
  94.                     bInString = false;
  95.                 }
  96.             }
  97.         }
  98.         else if (';' == chTmp && !bInString)
  99.         {
  100.             fseek(m_pfFile, m_iPos - iLen, SEEK_SET);
  101.             memset(srTmp, 0, sizeof(srTmp));
  102.             fread(srTmp, 1, iLen + 1, m_pfFile);
  103.             strncpy(psrStmt, srTmp, sizeof(srTmp));
  104.             CFileUnit::Erase(psrStmt);
  105.             m_iPos++;
  106.             return;
  107.         }
  108.         chPrev = chTmp;
  109.         iLen++;
  110.         m_iPos++;
  111.     }
  112.     m_bEndFile = true;
  113. }

  114. void CFileUnit::Erase(char* psrStmt)
  115. {
  116.     char* psrTra = psrStmt;
  117.     char* psrSt = psrStmt;

  118.     while (*psrTra != '\0')
  119.     {
  120.         if ('\n' == *psrTra || '\r' == *psrTra || ' ' == *psrTra)
  121.         {
  122.             psrTra++;
  123.         }
  124.         else
  125.         {
  126.             *psrSt++ = *psrTra++;
  127.         }
  128.     }
  129.     *psrSt = '\0';
  130. }

  131. int main()
  132. {
  133.     char str[128] = {'\0'};
  134.     CFileUnit fileUnit;

  135.     fileUnit.GetFilePath("tempfile");
  136.     fileUnit.OpenFile();

  137.     while (1)
  138.     {
  139.         fileUnit.GetSQLStatement(str);
  140.         if (fileUnit.isEndFile())
  141.         {
  142.             break;
  143.         }
  144.         cout << str << endl;
  145.     }

  146.     return 0;
  147. }


tempfile文件内容:'\'name\''; ';\';;;'; update('\'\'');
缺陷:
遇到的问题:第90行和对应的113行。设置srTmp大小为1024字节,这样113执行时,覆盖了对象中其他属性的值,被改为0了,导致之后的函数调用出现“Segment error”。strncpy(psrStmt, srTmp, sizeof(srTmp));也应该改为strncpy(psrStmt, srTmp, sizeof(psrStmt));
阅读(836) | 评论(0) | 转发(0) |
0

上一篇:最短路径问题

下一篇:没有了

给主人留下些什么吧!~~