自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Linux下控制(統(tǒng)計(jì))文件的生成的C代碼實(shí)現(xiàn)

開(kāi)發(fā) 開(kāi)發(fā)工具
這篇文章主要介紹Linux下控制(統(tǒng)計(jì))文件的生成的C代碼實(shí)現(xiàn)。

需求描述

不定時(shí)地在Linux機(jī)器下的某目錄中放入文件,文件內(nèi)容中包含了用戶(hù)號(hào)碼、起止時(shí)間等字段,現(xiàn)要求編寫(xiě)一程序來(lái)統(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è)文件中五條記錄的開(kāi)始時(shí)間的最小值為“20151101 110909”,結(jié)束時(shí)間的***值為“20160109 091545”,目前共處理了5條記錄。也就是說(shuō),控制文件的格式為:呼叫起始時(shí)間最小值|呼叫結(jié)束時(shí)間***值|記錄總條數(shù)|。

代碼

程序代碼

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

main.c

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

CtlFileCreate.h

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

CtlFileCreate.c

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

程序編譯及運(yùn)行

將程序代碼上傳到Linux機(jī)器上,并在當(dāng)前用戶(hù)的zhouzhaoxiong/zzx/CtlFileCreate/SrcFile目錄下上傳一些滿(mǎn)足命名規(guī)范的源文件,然后使用“gcc -g -o CtlFileCreate main.c CtlFileCreate.c”命令對(duì)程序進(jìn)行編譯,生成“CtlFileCreate”文件;接著運(yùn)行“CtlFileCreate”命令,可以看到在當(dāng)前用戶(hù)的zhouzhaoxiong/zzx/CtlFileCreate/CtlFile目錄下有控制文件生成,在當(dāng)前用戶(hù)的zhouzhaoxiong/zzx/CtlFileCreate/SrcFile_bak目錄下有源文件的備份文件生成。

查看控制文件內(nèi)容,里面記錄的就是當(dāng)前所處理的所有文件中的記錄總條數(shù)及所有記錄中的呼叫起始時(shí)間最小值和呼叫結(jié)束時(shí)間***值。

程序說(shuō)明

***,為了便于說(shuō)明,在本程序中,源文件的前綴是“Test_”,控制文件命名為“CtlFile.txt”。在實(shí)際的開(kāi)發(fā)中,大家完全可以通過(guò)配置項(xiàng)來(lái)決定源文件及控制文件的命名規(guī)則。

第二,為了防止源文件被重復(fù)處理,當(dāng)某個(gè)源文件處理完成之后,會(huì)被剪切到備份目錄中。這樣做也是為了方便之后校對(duì)控制文件中的內(nèi)容。

第三,在讀取文件中的***條記錄時(shí),將該條記錄中的呼叫起始時(shí)間和呼叫結(jié)束時(shí)間分別存放到兩個(gè)全局變量中,并按照格式寫(xiě)控制文件;在讀取該文件中的其他記錄時(shí),首先將該條記錄中的呼叫起始時(shí)間和呼叫結(jié)束時(shí)間與全局變量進(jìn)行比較,確保全局變量中存放的是呼叫起始時(shí)間最小值和呼叫結(jié)束時(shí)間***值,記錄總條數(shù)加1,并將新的記錄內(nèi)容寫(xiě)入到控制文件中。

第四,在處理完當(dāng)前目錄下的所有文件之后,程序會(huì)休眠一段時(shí)間,然后繼續(xù)掃描目錄。在實(shí)際的開(kāi)發(fā)中,休眠間隔也是可以配置的。

(本文中的部分代碼已經(jīng)提交到GitHub上,歡迎下載閱讀:

https://github.com/zhouzxi/CtlFileCreate)

【本文是51CTO專(zhuān)欄作者周兆熊的原創(chuàng)文章,作者微信公眾號(hào):周氏邏輯(logiczhou)】

戳這里,看該作者更多好文

責(zé)任編輯:趙寧寧 來(lái)源: 51CTO專(zhuān)欄
相關(guān)推薦

2009-08-18 13:35:06

C#枚舉文件

2011-01-19 15:03:24

Qmail控制文件

2009-04-03 10:25:32

C#XML擴(kuò)展代碼

2009-08-12 18:29:06

C#讀取TXT文件

2011-03-04 09:40:36

Vsftpd文件

2012-09-21 10:30:56

Linux項(xiàng)目代碼覆蓋率

2015-11-11 15:19:13

Linux編譯調(diào)試

2011-03-04 10:04:31

Linux文件操作命令

2009-06-26 10:43:57

生成EXCELJSF

2010-10-29 14:20:54

Oracle移動(dòng)控制文

2010-06-04 10:49:58

Linux流量控制

2010-08-02 14:29:46

LinuxPingICMP

2014-01-22 11:04:51

Linux流量監(jiān)控

2011-01-19 09:57:48

Postfix控制

2012-09-19 10:14:12

Visual Stud

2009-09-02 17:24:44

C#關(guān)機(jī)代碼

2024-03-06 08:52:59

C#Emit代碼

2023-09-04 11:46:09

C#動(dòng)態(tài)代碼

2024-01-07 16:42:32

C++編程開(kāi)發(fā)

2010-05-28 11:05:56

Linux下dhcp測(cè)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)