日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
Linux下控制(統(tǒng)計(jì))文件的生成的C代碼實(shí)現(xiàn)

需求描述

成都創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比上城網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫(kù),直接使用。一站式上城網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋上城地區(qū)。費(fèi)用合理售后完善,十多年實(shí)體公司更值得信賴。

不定時(shí)地在Linux機(jī)器下的某目錄中放入文件,文件內(nèi)容中包含了用戶號(hào)碼、起止時(shí)間等字段,現(xiàn)要求編寫一程序來統(tǒng)計(jì)目前該目錄中所有文件中的記錄總條數(shù)及所有記錄中的最早起始時(shí)間和最晚結(jié)束時(shí)間。

例如,該目錄中有兩個(gè)文件Test_1.txt和Test_2.txt,其中Test_1.txt文件內(nèi)容為:

 
 
  1. 15696192591|15696192592|20151103 120909|20151103 201545| 
  2. 15696192593|15696192594|20151103 110909|20151103 191545| 
  3. 02344273522|02344273523|20160108 110909|20160109 091545|

Test_2.txt文件內(nèi)容為:

 
 
  1. 15696192595|15696192596|20151102 120909|20151104 201545| 
  2. 15696192597|15696192598|20151101 110909|20151103 191545|

即文件中的每條記錄的格式為:呼叫號(hào)碼|被呼叫號(hào)碼|呼叫起始時(shí)間|呼叫結(jié)束時(shí)間|,要求生成的控制文件CtlFile.txt的內(nèi)容為:

 
 
  1. 20151101 110909|20160109 091545|5|

即Test_1.txt和Test_2.txt兩個(gè)文件中五條記錄的開始時(shí)間的最小值為“20151101 110909”,結(jié)束時(shí)間的***值為“20160109 091545”,目前共處理了5條記錄。也就是說,控制文件的格式為:呼叫起始時(shí)間最小值|呼叫結(jié)束時(shí)間***值|記錄總條數(shù)|。

程序代碼

本程序一共包括了三個(gè)代碼文件:main.c、CtlFileCreate.c和CtlFileCreate.h,具體代碼如下:

main.c

 
 
  1. /**********************************************************************
  2. * 版權(quán)所有 (C)2016, Zhou Zhaoxiong。
  3. *
  4. * 文件名稱:CtlFileCreate.c
  5. * 文件標(biāo)識(shí):無
  6. * 內(nèi)容摘要:目錄中文件的讀取及控制文件的生成
  7. * 其它說明:無
  8. * 當(dāng)前版本:V1.0
  9. * 作    者:Zhou Zhaoxiong
  10. * 完成日期:20160109
  11. *
  12. **********************************************************************/
  13. #include "CtlFileCreate.h"
  14. /**********************************************************************
  15. * 功能描述:主函數(shù)
  16. * 輸入?yún)?shù):無
  17. * 輸出參數(shù):無
  18. * 返 回 值:無
  19. * 其它說明:無
  20. * 修改日期        版本號(hào)      修改人            修改內(nèi)容
  21. * -------------------------------------------------------------------
  22. * 20160109        V1.0     Zhou Zhaoxiong        創(chuàng)建
  23. ***********************************************************************/
  24. INT32 main()
  25. {
  26.     ReadCtlFile();    // 獲取控制文件存放路徑、控制文件全路徑名及文件內(nèi)容字段值
  27.     ReadSrcFileAndWriteCtlFile();    // 掃描源文件目錄, 并寫控制文件
  28.     return 0;
  29. }

CtlFileCreate.h

 
 
  1. /**********************************************************************
  2. * 版權(quán)所有 (C)2015, Zhou Zhaoxiong。
  3. *
  4. * 文件名稱:CtlFileCreate.h
  5. * 文件標(biāo)識(shí):無
  6. * 內(nèi)容摘要:目錄中文件的讀取及控制文件的生成
  7. * 其它說明:無
  8. * 當(dāng)前版本:V1.0
  9. * 作    者:Zhou Zhaoxiong
  10. * 完成日期:20151102
  11. *
  12. **********************************************************************/
  13. #include 
  14. #include 
  15. #include 
  16. #include 
  17. // 數(shù)據(jù)類型重定義
  18. typedef unsigned char       UINT8;
  19. typedef unsigned short int  UINT16;
  20. typedef unsigned int        UINT32;
  21. typedef signed   int        INT32;
  22. typedef unsigned char       BOOL;
  23. // 參數(shù)類型
  24. #define MML_INT8_TYPE       0
  25. #define MML_INT16_TYPE      1
  26. #define MML_INT32_TYPE      2
  27. #define MML_STR_TYPE        3
  28. #define  TRUE         (BOOL)1
  29. #define  FALSE        (BOOL)0
  30. // 字段***長(zhǎng)度
  31. #define MAX_RET_BUF_LEN     1024
  32. // 源文件字段結(jié)構(gòu)體
  33. typedef struct
  34. {
  35.     UINT8 szSrcNumber[50];
  36.     UINT8 szDstNumber[50];
  37.     UINT8 szDataStartTime[50]; 
  38.     UINT8 szDataEndTime[50]; 
  39. } T_SrcFileContent;
  40. // 函數(shù)聲明
  41. void Sleep(UINT32 iCountMs);
  42. void ReadCtlFile(void);
  43. void ReadSrcFileAndWriteCtlFile(void);
  44. void GetSrcFileContentAndWriteCtlFile(UINT8 *pszSrcFileName);
  45. void GetSrcFileFieldValue(UINT8 *pszContentLine, T_SrcFileContent *ptSrcFileContent);
  46. void GetCtlFileContentAndWrite(T_SrcFileContent *ptSrcFileContent, UINT8 *pszContentBuffer);
  47. BOOL GetValueFromStr(UINT16 iSerialNum, UINT8 iContentType, UINT8 *pSourceStr, UINT8 *pDstStr, UINT8 cIsolater, UINT32 iDstStrSize);
  48. void RemoveLineEnd(UINT8 *pszStr);
  49. void WriteToCtlFile(UINT8 *pszContentLine); 

CtlFileCreate.c

 
 
  1. /**********************************************************************
  2. * 版權(quán)所有 (C)2015, Zhou Zhaoxiong。
  3. *
  4. * 文件名稱:CtlFileCreate.c
  5. * 文件標(biāo)識(shí):無
  6. * 內(nèi)容摘要:目錄中文件的讀取及控制文件的生成
  7. * 其它說明:無
  8. * 當(dāng)前版本:V1.0
  9. * 作    者:Zhou Zhaoxiong
  10. * 完成日期:20151102
  11. *
  12. **********************************************************************/
  13. #include "CtlFileCreate.h"
  14. // 全局變量
  15. UINT8  g_szSourceDir[500]    = {0};         // 需掃描的源目錄
  16. UINT8  g_szCtlFileDir[500]   = {0};         // 生成的控制文件的存放目錄
  17. UINT8  g_szSourceBakDir[500] = {0};         // 處理之后的源文件的備份目錄
  18. UINT8  g_szCtlFileName[256]  = {0};         // 控制文件全路徑名
  19. UINT8  g_szDataStartTime[50] = {0};         // 所有源文件中數(shù)據(jù)記錄的最早開始時(shí)間
  20. UINT8  g_szDataEndTime[50]   = {0};         // 所有源文件中數(shù)據(jù)記錄的最晚結(jié)束時(shí)間
  21. UINT32 g_iRecordsSum         = 0;           // 已處理的記錄的總條數(shù)
  22. /**********************************************************************
  23. * 功能描述: 讀取控制文件中的開始時(shí)間、結(jié)束時(shí)間和記錄條數(shù)
  24. * 輸入?yún)?shù): 無
  25. * 輸出參數(shù): 無
  26. * 返 回 值: 無
  27. * 其它說明: 無
  28. * 修改日期          版本號(hào)       修改人              修改內(nèi)容
  29. * ------------------------------------------------------------------
  30. * 20151102          V1.0     Zhou Zhaoxiong           創(chuàng)建
  31. ********************************************************************/
  32. void ReadCtlFile(void)
  33. {
  34.     UINT8 *pszHomePath = NULL; 
  35.     FILE  *fpCtlFile   = NULL;
  36.     UINT8  szBuf[500]  = {0}; 
  37.     // 讀取控制文件中的開始時(shí)間、結(jié)束時(shí)間和記錄條數(shù), 如果是當(dāng)天程序重啟, 則記錄條數(shù)繼續(xù)編號(hào)
  38.     pszHomePath = getenv("HOME");
  39.     if (pszHomePath == NULL)
  40.     {
  41.         return;
  42.     }
  43.     snprintf(g_szCtlFileDir, sizeof(g_szCtlFileDir)-1, "%s/zhouzhaoxiong/zzx/CtlFileCreate/CtlFile", pszHomePath);  // 控制文件存放目錄
  44.     snprintf(g_szCtlFileName, sizeof(g_szCtlFileName)-1, "%s/CtlFile.txt", g_szCtlFileDir);  // 控制文件全路徑名
  45.     fpCtlFile = fopen(g_szCtlFileName, "r");
  46.     if (fpCtlFile != NULL)
  47.     {
  48.         fgets(szBuf, sizeof(szBuf), fpCtlFile);
  49.         // 獲取開始時(shí)間g_szDataStartTime
  50.         if (TRUE != GetValueFromStr(1, MML_STR_TYPE, szBuf, g_szDataStartTime, '|', sizeof(g_szDataStartTime)))
  51.         {
  52.             printf("ReadCtlFile: exec GetValueFromStr to get g_szDataStartTime failed!\n");
  53.             return;
  54.         }
  55.         // 獲取結(jié)束時(shí)間g_szDataEndTime
  56.         if (TRUE != GetValueFromStr(2, MML_STR_TYPE, szBuf, g_szDataEndTime, '|', sizeof(g_szDataEndTime)))
  57.         {
  58.             printf("ReadCtlFile: exec GetValueFromStr to get g_szDataEndTime failed!\n");
  59.             return;
  60.         }
  61.         // 獲取記錄條數(shù)g_iRecordsSum
  62.         if (TRUE != GetValueFromStr(3, MML_INT32_TYPE, szBuf, (UINT8 *)&g_iRecordsSum, '|', sizeof(g_iRecordsSum)))
  63.         {
  64.             printf("ReadCtlFile: exec GetValueFromStr to get g_iRecordsSum failed!\n");
  65.             return;
  66.         }
  67.         fclose(fpCtlFile);
  68.         fpCtlFile = NULL;
  69.         printf("ReadCtlFile: DataStartTime=%s, DataEndTime=%s, RecordsSum=%d\n", g_szDataStartTime, g_szDataEndTime, g_iRecordsSum);
  70.     }
  71. }
  72. /**********************************************************************
  73. * 功能描述: 掃描源文件目錄, 并寫控制文件
  74. * 輸入?yún)?shù): 無
  75. * 輸出參數(shù): 無
  76. * 返 回 值: 無
  77. * 其它說明: 無
  78. * 修改日期          版本號(hào)       修改人              修改內(nèi)容
  79. * ------------------------------------------------------------------
  80. * 20151102          V1.0     Zhou Zhaoxiong           創(chuàng)建
  81. ********************************************************************/
  82. void ReadSrcFileAndWriteCtlFile(void)
  83. {
  84.     UINT8 *pszHomePath        = NULL; 
  85.     UINT8  szCommandBuf[500]  = {0}; 
  86.     UINT8  szSrcFile[500]     = {0}; 
  87.     DIR            *pDir    = NULL;
  88.     struct dirent  *pDirent = NULL;
  89.     pszHomePath = getenv("HOME");
  90.     if (pszHomePath == NULL)
  91.     {
  92.         return;
  93.     }
  94.     snprintf(g_szSourceDir,    sizeof(g_szSourceDir)-1,    "%s/zhouzhaoxiong/zzx/CtlFileCreate/SrcFile", pszHomePath);      // 源文件存放目錄
  95.     snprintf(g_szSourceBakDir, sizeof(g_szSourceBakDir)-1, "%s/zhouzhaoxiong/zzx/CtlFileCreate/SrcFile_bak", pszHomePath);  // 源文件備份目錄
  96.     while (1)
  97.     {   
  98.         pDir = opendir(g_szSourceDir);
  99.         if (NULL == pDir)
  100.         {
  101.             printf("ReadSrcFileAndWriteCtlFile: pDir is NULL!\n");
  102.             continue;
  103.         }
  104.         while ((pDirent = readdir(pDir)) != NULL)    // 掃描源目錄, 獲取文件名
  105.         {
  106.             if (strncmp(pDirent->d_name, "Test_", strlen("Test_")) == 0)      // 如果匹配上了源文件的前綴, 則讀取文件內(nèi)容并寫控制文件
  107.             {
  108.                 memset(szSrcFile, 0x00, sizeof(szSrcFile));
  109.                 snprintf(szSrcFile, sizeof(szSrcFile)-1, "%s/%s", g_szSourceDir, pDirent->d_name, g_szSourceBakDir);
  110.                 GetSrcFileContentAndWriteCtlFile(szSrcFile);     // 獲取源文件中的內(nèi)容, 并寫控制文件
  111.                 // 處理完成之后, 將文件剪切到備份目錄中
  112.                 memset(szCommandBuf, 0x00, sizeof(szCommandBuf));
  113.                 snprintf(szCommandBuf, sizeof(szCommandBuf)-1, "mv %s %s", szSrcFile, g_szSourceBakDir);
  114.                 system(szCommandBuf);
  115.                 printf("ReadSrcFileAndWriteCtlFile: now, move %s to %s\n", pDirent->d_name, g_szSourceBakDir);
  116.             }
  117.         }
  118.         closedir(pDir);
  119.         pDir = NULL;
  120.         Sleep(60 * 1000);    // 每1分鐘掃描一次
  121.     }
  122. }
  123. /**********************************************************************
  124. * 功能描述: 獲取源文件中的內(nèi)容, 并寫控制文件
  125. * 輸入?yún)?shù): pszSrcFileName-帶路徑的源文件名
  126. * 輸出參數(shù): 無
  127. * 返 回 值: 無
  128. * 其它說明: 無
  129. * 修改日期          版本號(hào)       修改人              修改內(nèi)容
  130. * ------------------------------------------------------------------
  131. * 20151102          V1.0     Zhou Zhaoxiong           創(chuàng)建
  132. ********************************************************************/
  133. void GetSrcFileContentAndWriteCtlFile(UINT8 *pszSrcFileName)
  134. {
  135.     FILE  *fp                  = NULL;
  136.     UINT8  szContentLine[1024] = {0};
  137.     T_SrcFileContent tSrcFileContent = {0};
  138.     if (pszSrcFileName == NULL)
  139.     {
  140.         printf("GetSrcFileContentAndWriteCtlFile: pDir is NULL!\n");
  141.         return;
  142.     }
  143.     if ((fp = fopen(pszSrcFileName, "r")) == NULL)  // 只讀方式打開
  144.     {
  145.         printf("GetSrcFileContentAndWriteCtlFile: open src file failed!\n");
  146.         return; 
  147.     }
  148.     else
  149.     {
  150.         while (feof(fp) == 0 && ferror(fp) == 0)
  151.         {
  152.             // 每行對(duì)應(yīng)一條源文件記錄
  153.             memset(szContentLine, 0x00, sizeof(szContentLine));
  154.             if (fgets(szContentLine, sizeof(szContentLine), fp) == NULL)
  155.             {
  156.                 printf("GetSrcFileContentAndWriteCtlFile: exec fgets to get line null.\n");
  157.             }
  158.             else
  159.             {
  160.                 printf("GetSrcFileContentAndWriteCtlFile: get content line: %s\n", szContentLine);
  161.             }
  162.             RemoveLineEnd(szContentLine); // 去掉字符串后面的回車換行符
  163.             if (strlen(szContentLine) == 0)   // 如果為空行, 則繼續(xù)處理下一條
  164.             {
  165.                 printf("GetSrcFileContentAndWriteCtlFile: the length of ContentLine is 0, continue.\n"); 
  166.                 continue;
  167.             }
  168.             GetSrcFileFieldValue(szContentLine, &tSrcFileContent);   // 獲取一條記錄中各個(gè)字段的值
  169.             memset(szContentLine, 0x00, sizeof(szContentLine));
  170.             GetCtlFileContentAndWrite(&tSrcFileContent, szContentLine); // 組裝寫入控制文件中的內(nèi)容
  171.             WriteToCtlFile(szContentLine);    // 將內(nèi)容寫到控制文件中
  172.         }
  173.         fclose(fp);
  174.         fp = NULL;
  175.     }
  176. }
  177. /**********************************************************************
  178. * 功能描述: 組裝寫入控制文件中的內(nèi)容
  179. * 輸入?yún)?shù): ptSrcFileContent-源文件中一條記錄中各個(gè)字段的值
  180. * 輸出參數(shù): pszContentBuffer-存放內(nèi)容的緩存
  181. * 返 回 值: 無
  182. * 其它說明: 控制文件中記錄為: DataStartTime|DataEndTime|RecordsSum|
  183. * 修改日期          版本號(hào)       修改人              修改內(nèi)容
  184. * ------------------------------------------------------------------
  185. * 20151102          V1.0     Zhou Zhaoxiong           創(chuàng)建
  186. ********************************************************************/
  187. void GetCtlFileContentAndWrite(T_SrcFileContent *ptSrcFileContent, UINT8 *pszContentBuffer)
  188. {
  189.     UINT8  szContentLine[500] = {0};
  190.     if (ptSrcFileContent == NULL || pszContentBuffer == NULL)
  191.     {
  192.         printf("GetCtlFileContentAndWrite: ptSrcFileContent or pszContentBuffer is NULL!\n");
  193.         return;
  194.     }
  195.     // 根據(jù)值的大小對(duì)g_szDataStartTime進(jìn)行賦值
  196.     if (strlen(g_szDataStartTime) == 0)   // 當(dāng)天***條
  197.     {
  198.         strncpy(g_szDataStartTime, ptSrcFileContent->szDataStartTime, strlen(ptSrcFileContent->szDataStartTime));
  199.     }
  200.     else
  201.     {
  202.         if (strncmp(g_szDataStartTime, ptSrcFileContent->szDataStartTime, strlen(ptSrcFileContent->szDataStartTime)) > 0)  // 修改成最小時(shí)間
  203.         {
  204.             memset(g_szDataStartTime, 0x00, sizeof(g_szDataStartTime));
  205.             strncpy(g_szDataStartTime, ptSrcFileContent->szDataStartTime, strlen(ptSrcFileContent->szDataStartTime));
  206.         }
  207.     }
  208.     // 根據(jù)值的大小對(duì)g_szDataEndTime進(jìn)行賦值
  209.     if (strlen(g_szDataEndTime) == 0)   // 當(dāng)天***條
  210.     {
  211.         strncpy(g_szDataEndTime, ptSrcFileContent->szDataEndTime, strlen(ptSrcFileContent->szDataEndTime));
  212.     }
  213.     else
  214.     {
  215.         if (strncmp(g_szDataEndTime, ptSrcFileContent->szDataEndTime, strlen(ptSrcFileContent->szDataEndTime)) < 0)  // 修改成***時(shí)間
  216.         {
  217.             memset(g_szDataEndTime, 0x00, sizeof(g_szDataEndTime));
  218.             strncpy(g_szDataEndTime, ptSrcFileContent->szDataEndTime, strlen(ptSrcFileContent->szDataEndTime));
  219.         }
  220.     }
  221.     // 記錄總條數(shù)加1
  222.     g_iRecordsSumg_iRecordsSum = g_iRecordsSum + 1;     // 當(dāng)天所有記錄的總條數(shù)加1
  223.     // 打印三個(gè)字段的內(nèi)容
  224.     printf("GetCtlFileContentAndWrite: DataStartTime is %s, DataEndTime is %s, RecordsSum is %d\n", g_szDataStartTime, g_szDataEndTime, g_iRecordsSum);
  225.     // 組裝寫到控制文件中的消息內(nèi)容
  226.     snprintf(szContentLine, sizeof(szContentLine)-1, "%s|%s|%d|", g_szDataStartTime, g_szDataEndTime, g_iRecordsSum);
  227.     printf("GetCtlFileContentAndWrite: ContentLine is %s\n", szContentLine);
  228.     strncpy(pszContentBuffer, szContentLine, strlen(szContentLine));
  229. }
  230. /**********************************************************************
  231. * 功能描述: 獲取源文件中的各個(gè)字段的值
  232. * 輸入?yún)?shù): pszContentLine-一條記錄
  233. * 輸出參數(shù): ptSrcFileContent-源文件中一條記錄中各個(gè)字段的值
  234. * 返 回 值: 無
  235. * 其它說明: 源文件中每條記錄的格式為: SrcNumber|DstNumber|DataStartTime|DataEndTime|
  236. * 修改日期          版本號(hào)       修改人              修改內(nèi)容
  237. * ------------------------------------------------------------------
  238. * 20151102          V1.0     Zhou Zhaoxiong           創(chuàng)建
  239. ********************************************************************/
  240. void GetSrcFileFieldValue(UINT8 *pszContentLine, T_SrcFileContent *ptSrcFileContent)
  241. {
  242.     if (pszContentLine == NULL || ptSrcFileContent == NULL)
  243.     {
  244.         printf("GetSrcFileFieldValue: ContentLine or SrcFileContent is NULL!\n");
  245.         return;
  246.     }
  247.     // 獲取源號(hào)碼
  248.     if (TRUE != GetValueFromStr(1, MML_STR_TYPE, pszContentLine, ptSrcFileContent->szSrcNumber, '|', sizeof(ptSrcFileContent->szSrcNumber)))
  249.     {
  250.         printf("GetSrcFileFieldValue: exec GetValueFromStr to get szSrcNumber failed!\n");
  251.         return;
  252.     }
  253.     // 獲取目的號(hào)碼
  254.     if (TRUE != GetValueFromStr(2, MML_STR_TYPE, pszContentLine, ptSrcFileContent->szDstNumber, '|', sizeof(ptSrcFileContent->szDstNumber)))
  255.     {
  256.         printf("GetSrcFileFieldValue: exec GetValueFromStr to get szDstNumber failed!\n");
  257.         return;
  258.     }
  259.     // 獲取開始時(shí)間
  260.     if (TRUE != GetValueFromStr(3, MML_STR_TYPE, pszContentLine, ptSrcFileContent->szDataStartTime, '|', sizeof(ptSrcFileContent->szDataStartTime)))
  261.     {
  262.         printf("GetSrcFileFieldValue: exec GetValueFromStr to get szDataStartTime failed!\n");
  263.         return;
  264.     }
  265.     // 獲取結(jié)束時(shí)間
  266.     if (TRUE != GetValueFromStr(4, MML_STR_TYPE, pszContentLine, ptSrcFileContent->szDataEndTime, '|', sizeof(ptSrcFileContent->szDataEndTime)))
  267.     {
  268.         printf("GetSrcFileFieldValue: exec GetValueFromStr to get szDataEndTime failed!\n");
  269.         return;
  270.     }
  271.     printf("GetSrcFileFieldValue: SrcNumber=%s, DstNumber=%s, DataStartTime=%s, DataEndTime=%s\n", ptSrcFileContent->szSrcNumber, ptSrcFileContent->szDstNumber, 
  272.                                                                                 ptSrcFileContent->szDataStartTime, ptSrcFileContent->szDataEndTime);
  273. }
  274. /**********************************************************************
  275. * 功能描述: 程序休眠
  276. * 輸入?yún)?shù): iCountMs-休眠時(shí)間(單位:ms)
  277. * 輸出參數(shù): 無
  278. * 返 回 值: 無
  279. * 其它說明: 無
  280. * 修改日期          版本號(hào)       修改人              修改內(nèi)容
  281. * ------------------------------------------------------------------
  282. * 20151102          V1.0     Zhou Zhaoxiong           創(chuàng)建
  283. ********************************************************************/
  284. void Sleep(UINT32 iCountMs)
  285. {
  286.     struct timeval t_timeout = {0};
  287.     if (iCountMs < 1000)
  288.     {
  289.         t_timeout.tv_sec = 0;
  290.         t_timeout.tv_usec = iCountMs * 1000;
  291.     }
  292.     else
  293.     {
  294.         t_timeout.tv_sec = iCountMs / 1000;
  295.         t_timeout.tv_usec = (iCountMs % 1000) * 1000;
  296.     }
  297.     select(0, NULL, NULL, NULL, &t_timeout);   // 調(diào)用select函數(shù)阻塞程序
  298. }
  299. /**********************************************************************
  300. *功能描述:獲取字符串中某一個(gè)字段的值
  301. *輸入?yún)?shù):iSerialNum-字段編號(hào)(為正整數(shù))
  302.            iContentType-需要獲取的內(nèi)容的類型
  303.            pSourceStr-源字符串
  304.            pDstStr-目的字符串(提取的值的存放位置)
  305.            cIsolater-源字符串中字段的分隔符
  306.            iDstStrSize-目的字符串的長(zhǎng)度
  307. *輸出參數(shù):無
  308. *返 回 值:TRUE-成功  FALSE-失敗
  309. *其它說明:無
  310. *修改日期        版本號(hào)            修改人         修改內(nèi)容
  311. * --------------------------------------------------------------
  312. * 20151102        V1.0         Zhou Zhaoxiong       創(chuàng)建
  313. ***********************************************************************/
  314. BOOL GetValueFromStr(UINT16 iSerialNum, UINT8 iContentType, UINT8 *pSourceStr, UINT8 *pDstStr, UINT8 cIsolater, UINT32 iDstStrSize)
  315. {
  316.     UINT8  *pStrBegin                 = NULL;
  317.     UINT8  *pStrEnd                   = NULL;
  318.     UINT8   szRetBuf[MAX_RET_BUF_LEN] = {0};     // 截取出的字符串放入該數(shù)組中
  319.     UINT8  *pUINT8                    = NULL;
  320.     UINT16 *pUINT16                   = NULL;
  321.     UINT32 *pUINT32                   = NULL;
  322.     UINT32  iFieldLen                 = 0;      // 用于表示每個(gè)字段的實(shí)際長(zhǎng)度
  323.     if (pSourceStr == NULL)                     // 對(duì)輸入指針的異常情況進(jìn)行判斷
  324.     {
  325.         return FALSE;
  326.     }
  327.     //字段首
  328.     pStrBegin = pSourceStr;
  329.     while (--iSerialNum != 0)
  330.     {
  331.         pStrBegin = strchr(pStrBegin, cIsolater);
  332.         if (pStrBegin == NULL)
  333.         {
  334.             return FALSE;
  335.         }
  336.         pStrBegin ++;
  337.     }
  338.     //字段尾
  339.     pStrEnd = strchr(pStrBegin, cIsolater);
  340.     if (pStrEnd == NULL)
  341.     {
  342.         return FALSE;
  343.     }
  344.     iFieldLen = (UINT16)(pStrEnd - pStrBegin);
  345.     if(iFieldLen >= MAX_RET_BUF_LEN) //進(jìn)行異常保護(hù), 防止每個(gè)字段的值過長(zhǎng)
  346.     {
  347.         iFieldLen = MAX_RET_BUF_LEN - 1;
  348.     }
  349.     memcpy(szRetBuf, pStrBegin, iFieldLen);
  350.     //將需要的字段值放到pDstStr中去
  351.     switch (iContentType)
  352.     {
  353.         case MML_STR_TYPE:                        //字符串類型
  354.         {
  355.             strncpy(pDstStr, szRetBuf, iDstStrSize);
  356.             break;
  357.         }
  358.         case MML_INT8_TYPE:                       //字符類型
  359.         {
  360.             pUINT8   = (UINT8 *)pDstStr;
  361.             *pDstStr = (UINT8)atoi(szRetBuf);
  362.             break;
  363.         }
  364.         case MML_INT16_TYPE:                      // short int類型
  365.         {
  366.             pUINT16  = (UINT16 *)pDstStr;
  367.             *pUINT16 = (UINT16)atoi(szRetBuf);
  368.             break;
  369.         }
  370.         case MML_INT32_TYPE:                      // int類型
  371.         {
  372.             pUINT32  = (UINT32 *)pDstStr;
  373.             *pUINT32 = (UINT32)atoi(szRetBuf);
  374.             break;
  375.         }
  376.         default:                                  // 一定要有default分支
  377.         {
  378.             return FALSE;
  379.         }
  380.     }
  381.     return TRUE;
  382. }
  383. /**********************************************************************
  384. * 功能描述: 去掉字符串后面的回車換行符
  385. * 輸入?yún)?shù): pszStr-輸入的字符串
  386. * 輸出參數(shù): 無
  387. * 返 回 值: 無
  388. * 其它說明: 無
  389. * 修改日期          版本號(hào)       修改人              修改內(nèi)容
  390. * ------------------------------------------------------------------
  391. * 20151102          V1.0     Zhou Zhaoxiong           創(chuàng)建
  392. ********************************************************************/
  393. void RemoveLineEnd(UINT8 *pszStr)
  394. {
  395.     UINT32  iStrLen = 0;
  396.     if (pszStr == NULL)
  397.     {
  398.         printf("RemoveLineEnd: pszStr is NULL!\n");
  399.         return;
  400.     }
  401.     iStrLen = strlen(pszStr);
  402. 網(wǎng)站欄目:Linux下控制(統(tǒng)計(jì))文件的生成的C代碼實(shí)現(xiàn)
    文章地址:http://m.5511xx.com/article/djjhiho.html