Linux下控制(統計)文件的生成的C代碼實現
需求描述
不定時地在Linux機器下的某目錄中放入文件,文件內容中包含了用戶號碼、起止時間等字段,現要求編寫一程序來統計目前該目錄中所有文件中的記錄總條數及所有記錄中的最早起始時間和最晚結束時間。
例如,該目錄中有兩個文件Test_1.txt和Test_2.txt,其中Test_1.txt文件內容為:
- 15696192591|15696192592|20151103 120909|20151103 201545|
- 15696192593|15696192594|20151103 110909|20151103 191545|
- 02344273522|02344273523|20160108 110909|20160109 091545|
Test_2.txt文件內容為:
- 15696192595|15696192596|20151102 120909|20151104 201545|
- 15696192597|15696192598|20151101 110909|20151103 191545|
即文件中的每條記錄的格式為:呼叫號碼|被呼叫號碼|呼叫起始時間|呼叫結束時間|,要求生成的控制文件CtlFile.txt的內容為:
- 20151101 110909|20160109 091545|5|
即Test_1.txt和Test_2.txt兩個文件中五條記錄的開始時間的最小值為“20151101 110909”,結束時間的***值為“20160109 091545”,目前共處理了5條記錄。也就是說,控制文件的格式為:呼叫起始時間最小值|呼叫結束時間***值|記錄總條數|。
程序代碼
本程序一共包括了三個代碼文件:main.c、CtlFileCreate.c和CtlFileCreate.h,具體代碼如下:
main.c
- /**********************************************************************
- * 版權所有 (C)2016, Zhou Zhaoxiong。
- *
- * 文件名稱:CtlFileCreate.c
- * 文件標識:無
- * 內容摘要:目錄中文件的讀取及控制文件的生成
- * 其它說明:無
- * 當前版本:V1.0
- * 作 者:Zhou Zhaoxiong
- * 完成日期:20160109
- *
- **********************************************************************/
- #include "CtlFileCreate.h"
- /**********************************************************************
- * 功能描述:主函數
- * 輸入參數:無
- * 輸出參數:無
- * 返 回 值:無
- * 其它說明:無
- * 修改日期 版本號 修改人 修改內容
- * -------------------------------------------------------------------
- * 20160109 V1.0 Zhou Zhaoxiong 創建
- ***********************************************************************/
- INT32 main()
- {
- ReadCtlFile(); // 獲取控制文件存放路徑、控制文件全路徑名及文件內容字段值
- ReadSrcFileAndWriteCtlFile(); // 掃描源文件目錄, 并寫控制文件
- return 0;
- }
CtlFileCreate.h
- /**********************************************************************
- * 版權所有 (C)2015, Zhou Zhaoxiong。
- *
- * 文件名稱:CtlFileCreate.h
- * 文件標識:無
- * 內容摘要:目錄中文件的讀取及控制文件的生成
- * 其它說明:無
- * 當前版本:V1.0
- * 作 者:Zhou Zhaoxiong
- * 完成日期:20151102
- *
- **********************************************************************/
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <dirent.h>
- // 數據類型重定義
- typedef unsigned char UINT8;
- typedef unsigned short int UINT16;
- typedef unsigned int UINT32;
- typedef signed int INT32;
- typedef unsigned char BOOL;
- // 參數類型
- #define MML_INT8_TYPE 0
- #define MML_INT16_TYPE 1
- #define MML_INT32_TYPE 2
- #define MML_STR_TYPE 3
- #define TRUE (BOOL)1
- #define FALSE (BOOL)0
- // 字段***長度
- #define MAX_RET_BUF_LEN 1024
- // 源文件字段結構體
- typedef struct
- {
- UINT8 szSrcNumber[50];
- UINT8 szDstNumber[50];
- UINT8 szDataStartTime[50];
- UINT8 szDataEndTime[50];
- } T_SrcFileContent;
- // 函數聲明
- void Sleep(UINT32 iCountMs);
- void ReadCtlFile(void);
- void ReadSrcFileAndWriteCtlFile(void);
- void GetSrcFileContentAndWriteCtlFile(UINT8 *pszSrcFileName);
- void GetSrcFileFieldValue(UINT8 *pszContentLine, T_SrcFileContent *ptSrcFileContent);
- void GetCtlFileContentAndWrite(T_SrcFileContent *ptSrcFileContent, UINT8 *pszContentBuffer);
- BOOL GetValueFromStr(UINT16 iSerialNum, UINT8 iContentType, UINT8 *pSourceStr, UINT8 *pDstStr, UINT8 cIsolater, UINT32 iDstStrSize);
- void RemoveLineEnd(UINT8 *pszStr);
- void WriteToCtlFile(UINT8 *pszContentLine);
CtlFileCreate.c
- /**********************************************************************
- * 版權所有 (C)2015, Zhou Zhaoxiong。
- *
- * 文件名稱:CtlFileCreate.c
- * 文件標識:無
- * 內容摘要:目錄中文件的讀取及控制文件的生成
- * 其它說明:無
- * 當前版本:V1.0
- * 作 者:Zhou Zhaoxiong
- * 完成日期:20151102
- *
- **********************************************************************/
- #include "CtlFileCreate.h"
- // 全局變量
- UINT8 g_szSourceDir[500] = {0}; // 需掃描的源目錄
- UINT8 g_szCtlFileDir[500] = {0}; // 生成的控制文件的存放目錄
- UINT8 g_szSourceBakDir[500] = {0}; // 處理之后的源文件的備份目錄
- UINT8 g_szCtlFileName[256] = {0}; // 控制文件全路徑名
- UINT8 g_szDataStartTime[50] = {0}; // 所有源文件中數據記錄的最早開始時間
- UINT8 g_szDataEndTime[50] = {0}; // 所有源文件中數據記錄的最晚結束時間
- UINT32 g_iRecordsSum = 0; // 已處理的記錄的總條數
- /**********************************************************************
- * 功能描述: 讀取控制文件中的開始時間、結束時間和記錄條數
- * 輸入參數: 無
- * 輸出參數: 無
- * 返 回 值: 無
- * 其它說明: 無
- * 修改日期 版本號 修改人 修改內容
- * ------------------------------------------------------------------
- * 20151102 V1.0 Zhou Zhaoxiong 創建
- ********************************************************************/
- void ReadCtlFile(void)
- {
- UINT8 *pszHomePath = NULL;
- FILE *fpCtlFile = NULL;
- UINT8 szBuf[500] = {0};
- // 讀取控制文件中的開始時間、結束時間和記錄條數, 如果是當天程序重啟, 則記錄條數繼續編號
- pszHomePath = getenv("HOME");
- if (pszHomePath == NULL)
- {
- return;
- }
- snprintf(g_szCtlFileDir, sizeof(g_szCtlFileDir)-1, "%s/zhouzhaoxiong/zzx/CtlFileCreate/CtlFile", pszHomePath); // 控制文件存放目錄
- snprintf(g_szCtlFileName, sizeof(g_szCtlFileName)-1, "%s/CtlFile.txt", g_szCtlFileDir); // 控制文件全路徑名
- fpCtlFile = fopen(g_szCtlFileName, "r");
- if (fpCtlFile != NULL)
- {
- fgets(szBuf, sizeof(szBuf), fpCtlFile);
- // 獲取開始時間g_szDataStartTime
- if (TRUE != GetValueFromStr(1, MML_STR_TYPE, szBuf, g_szDataStartTime, '|', sizeof(g_szDataStartTime)))
- {
- printf("ReadCtlFile: exec GetValueFromStr to get g_szDataStartTime failed!\n");
- return;
- }
- // 獲取結束時間g_szDataEndTime
- if (TRUE != GetValueFromStr(2, MML_STR_TYPE, szBuf, g_szDataEndTime, '|', sizeof(g_szDataEndTime)))
- {
- printf("ReadCtlFile: exec GetValueFromStr to get g_szDataEndTime failed!\n");
- return;
- }
- // 獲取記錄條數g_iRecordsSum
- if (TRUE != GetValueFromStr(3, MML_INT32_TYPE, szBuf, (UINT8 *)&g_iRecordsSum, '|', sizeof(g_iRecordsSum)))
- {
- printf("ReadCtlFile: exec GetValueFromStr to get g_iRecordsSum failed!\n");
- return;
- }
- fclose(fpCtlFile);
- fpCtlFile = NULL;
- printf("ReadCtlFile: DataStartTime=%s, DataEndTime=%s, RecordsSum=%d\n", g_szDataStartTime, g_szDataEndTime, g_iRecordsSum);
- }
- }
- /**********************************************************************
- * 功能描述: 掃描源文件目錄, 并寫控制文件
- * 輸入參數: 無
- * 輸出參數: 無
- * 返 回 值: 無
- * 其它說明: 無
- * 修改日期 版本號 修改人 修改內容
- * ------------------------------------------------------------------
- * 20151102 V1.0 Zhou Zhaoxiong 創建
- ********************************************************************/
- void ReadSrcFileAndWriteCtlFile(void)
- {
- UINT8 *pszHomePath = NULL;
- UINT8 szCommandBuf[500] = {0};
- UINT8 szSrcFile[500] = {0};
- DIR *pDir = NULL;
- struct dirent *pDirent = NULL;
- pszHomePath = getenv("HOME");
- if (pszHomePath == NULL)
- {
- return;
- }
- snprintf(g_szSourceDir, sizeof(g_szSourceDir)-1, "%s/zhouzhaoxiong/zzx/CtlFileCreate/SrcFile", pszHomePath); // 源文件存放目錄
- snprintf(g_szSourceBakDir, sizeof(g_szSourceBakDir)-1, "%s/zhouzhaoxiong/zzx/CtlFileCreate/SrcFile_bak", pszHomePath); // 源文件備份目錄
- while (1)
- {
- pDir = opendir(g_szSourceDir);
- if (NULL == pDir)
- {
- printf("ReadSrcFileAndWriteCtlFile: pDir is NULL!\n");
- continue;
- }
- while ((pDirent = readdir(pDir)) != NULL) // 掃描源目錄, 獲取文件名
- {
- if (strncmp(pDirent->d_name, "Test_", strlen("Test_")) == 0) // 如果匹配上了源文件的前綴, 則讀取文件內容并寫控制文件
- {
- memset(szSrcFile, 0x00, sizeof(szSrcFile));
- snprintf(szSrcFile, sizeof(szSrcFile)-1, "%s/%s", g_szSourceDir, pDirent->d_name, g_szSourceBakDir);
- GetSrcFileContentAndWriteCtlFile(szSrcFile); // 獲取源文件中的內容, 并寫控制文件
- // 處理完成之后, 將文件剪切到備份目錄中
- memset(szCommandBuf, 0x00, sizeof(szCommandBuf));
- snprintf(szCommandBuf, sizeof(szCommandBuf)-1, "mv %s %s", szSrcFile, g_szSourceBakDir);
- system(szCommandBuf);
- printf("ReadSrcFileAndWriteCtlFile: now, move %s to %s\n", pDirent->d_name, g_szSourceBakDir);
- }
- }
- closedir(pDir);
- pDir = NULL;
- Sleep(60 * 1000); // 每1分鐘掃描一次
- }
- }
- /**********************************************************************
- * 功能描述: 獲取源文件中的內容, 并寫控制文件
- * 輸入參數: pszSrcFileName-帶路徑的源文件名
- * 輸出參數: 無
- * 返 回 值: 無
- * 其它說明: 無
- * 修改日期 版本號 修改人 修改內容
- * ------------------------------------------------------------------
- * 20151102 V1.0 Zhou Zhaoxiong 創建
- ********************************************************************/
- void GetSrcFileContentAndWriteCtlFile(UINT8 *pszSrcFileName)
- {
- FILE *fp = NULL;
- UINT8 szContentLine[1024] = {0};
- T_SrcFileContent tSrcFileContent = {0};
- if (pszSrcFileName == NULL)
- {
- printf("GetSrcFileContentAndWriteCtlFile: pDir is NULL!\n");
- return;
- }
- if ((fp = fopen(pszSrcFileName, "r")) == NULL) // 只讀方式打開
- {
- printf("GetSrcFileContentAndWriteCtlFile: open src file failed!\n");
- return;
- }
- else
- {
- while (feof(fp) == 0 && ferror(fp) == 0)
- {
- // 每行對應一條源文件記錄
- memset(szContentLine, 0x00, sizeof(szContentLine));
- if (fgets(szContentLine, sizeof(szContentLine), fp) == NULL)
- {
- printf("GetSrcFileContentAndWriteCtlFile: exec fgets to get line null.\n");
- }
- else
- {
- printf("GetSrcFileContentAndWriteCtlFile: get content line: %s\n", szContentLine);
- }
- RemoveLineEnd(szContentLine); // 去掉字符串后面的回車換行符
- if (strlen(szContentLine) == 0) // 如果為空行, 則繼續處理下一條
- {
- printf("GetSrcFileContentAndWriteCtlFile: the length of ContentLine is 0, continue.\n");
- continue;
- }
- GetSrcFileFieldValue(szContentLine, &tSrcFileContent); // 獲取一條記錄中各個字段的值
- memset(szContentLine, 0x00, sizeof(szContentLine));
- GetCtlFileContentAndWrite(&tSrcFileContent, szContentLine); // 組裝寫入控制文件中的內容
- WriteToCtlFile(szContentLine); // 將內容寫到控制文件中
- }
- fclose(fp);
- fp = NULL;
- }
- }
- /**********************************************************************
- * 功能描述: 組裝寫入控制文件中的內容
- * 輸入參數: ptSrcFileContent-源文件中一條記錄中各個字段的值
- * 輸出參數: pszContentBuffer-存放內容的緩存
- * 返 回 值: 無
- * 其它說明: 控制文件中記錄為: DataStartTime|DataEndTime|RecordsSum|
- * 修改日期 版本號 修改人 修改內容
- * ------------------------------------------------------------------
- * 20151102 V1.0 Zhou Zhaoxiong 創建
- ********************************************************************/
- void GetCtlFileContentAndWrite(T_SrcFileContent *ptSrcFileContent, UINT8 *pszContentBuffer)
- {
- UINT8 szContentLine[500] = {0};
- if (ptSrcFileContent == NULL || pszContentBuffer == NULL)
- {
- printf("GetCtlFileContentAndWrite: ptSrcFileContent or pszContentBuffer is NULL!\n");
- return;
- }
- // 根據值的大小對g_szDataStartTime進行賦值
- if (strlen(g_szDataStartTime) == 0) // 當天***條
- {
- strncpy(g_szDataStartTime, ptSrcFileContent->szDataStartTime, strlen(ptSrcFileContent->szDataStartTime));
- }
- else
- {
- if (strncmp(g_szDataStartTime, ptSrcFileContent->szDataStartTime, strlen(ptSrcFileContent->szDataStartTime)) > 0) // 修改成最小時間
- {
- memset(g_szDataStartTime, 0x00, sizeof(g_szDataStartTime));
- strncpy(g_szDataStartTime, ptSrcFileContent->szDataStartTime, strlen(ptSrcFileContent->szDataStartTime));
- }
- }
- // 根據值的大小對g_szDataEndTime進行賦值
- if (strlen(g_szDataEndTime) == 0) // 當天***條
- {
- strncpy(g_szDataEndTime, ptSrcFileContent->szDataEndTime, strlen(ptSrcFileContent->szDataEndTime));
- }
- else
- {
- if (strncmp(g_szDataEndTime, ptSrcFileContent->szDataEndTime, strlen(ptSrcFileContent->szDataEndTime)) < 0) // 修改成***時間
- {
- memset(g_szDataEndTime, 0x00, sizeof(g_szDataEndTime));
- strncpy(g_szDataEndTime, ptSrcFileContent->szDataEndTime, strlen(ptSrcFileContent->szDataEndTime));
- }
- }
- // 記錄總條數加1
- g_iRecordsSumg_iRecordsSum = g_iRecordsSum + 1; // 當天所有記錄的總條數加1
- // 打印三個字段的內容
- printf("GetCtlFileContentAndWrite: DataStartTime is %s, DataEndTime is %s, RecordsSum is %d\n", g_szDataStartTime, g_szDataEndTime, g_iRecordsSum);
- // 組裝寫到控制文件中的消息內容
- snprintf(szContentLine, sizeof(szContentLine)-1, "%s|%s|%d|", g_szDataStartTime, g_szDataEndTime, g_iRecordsSum);
- printf("GetCtlFileContentAndWrite: ContentLine is %s\n", szContentLine);
- strncpy(pszContentBuffer, szContentLine, strlen(szContentLine));
- }
- /**********************************************************************
- * 功能描述: 獲取源文件中的各個字段的值
- * 輸入參數: pszContentLine-一條記錄
- * 輸出參數: ptSrcFileContent-源文件中一條記錄中各個字段的值
- * 返 回 值: 無
- * 其它說明: 源文件中每條記錄的格式為: SrcNumber|DstNumber|DataStartTime|DataEndTime|
- * 修改日期 版本號 修改人 修改內容
- * ------------------------------------------------------------------
- * 20151102 V1.0 Zhou Zhaoxiong 創建
- ********************************************************************/
- void GetSrcFileFieldValue(UINT8 *pszContentLine, T_SrcFileContent *ptSrcFileContent)
- {
- if (pszContentLine == NULL || ptSrcFileContent == NULL)
- {
- printf("GetSrcFileFieldValue: ContentLine or SrcFileContent is NULL!\n");
- return;
- }
- // 獲取源號碼
- if (TRUE != GetValueFromStr(1, MML_STR_TYPE, pszContentLine, ptSrcFileContent->szSrcNumber, '|', sizeof(ptSrcFileContent->szSrcNumber)))
- {
- printf("GetSrcFileFieldValue: exec GetValueFromStr to get szSrcNumber failed!\n");
- return;
- }
- // 獲取目的號碼
- if (TRUE != GetValueFromStr(2, MML_STR_TYPE, pszContentLine, ptSrcFileContent->szDstNumber, '|', sizeof(ptSrcFileContent->szDstNumber)))
- {
- printf("GetSrcFileFieldValue: exec GetValueFromStr to get szDstNumber failed!\n");
- return;
- }
- // 獲取開始時間
- if (TRUE != GetValueFromStr(3, MML_STR_TYPE, pszContentLine, ptSrcFileContent->szDataStartTime, '|', sizeof(ptSrcFileContent->szDataStartTime)))
- {
- printf("GetSrcFileFieldValue: exec GetValueFromStr to get szDataStartTime failed!\n");
- return;
- }
- // 獲取結束時間
- if (TRUE != GetValueFromStr(4, MML_STR_TYPE, pszContentLine, ptSrcFileContent->szDataEndTime, '|', sizeof(ptSrcFileContent->szDataEndTime)))
- {
- printf("GetSrcFileFieldValue: exec GetValueFromStr to get szDataEndTime failed!\n");
- return;
- }
- printf("GetSrcFileFieldValue: SrcNumber=%s, DstNumber=%s, DataStartTime=%s, DataEndTime=%s\n", ptSrcFileContent->szSrcNumber, ptSrcFileContent->szDstNumber,
- ptSrcFileContent->szDataStartTime, ptSrcFileContent->szDataEndTime);
- }
- /**********************************************************************
- * 功能描述: 程序休眠
- * 輸入參數: iCountMs-休眠時間(單位:ms)
- * 輸出參數: 無
- * 返 回 值: 無
- * 其它說明: 無
- * 修改日期 版本號 修改人 修改內容
- * ------------------------------------------------------------------
- * 20151102 V1.0 Zhou Zhaoxiong 創建
- ********************************************************************/
- void Sleep(UINT32 iCountMs)
- {
- struct timeval t_timeout = {0};
- if (iCountMs < 1000)
- {
- t_timeout.tv_sec = 0;
- t_timeout.tv_usec = iCountMs * 1000;
- }
- else
- {
- t_timeout.tv_sec = iCountMs / 1000;
- t_timeout.tv_usec = (iCountMs % 1000) * 1000;
- }
- select(0, NULL, NULL, NULL, &t_timeout); // 調用select函數阻塞程序
- }
- /**********************************************************************
- *功能描述:獲取字符串中某一個字段的值
- *輸入參數:iSerialNum-字段編號(為正整數)
- iContentType-需要獲取的內容的類型
- pSourceStr-源字符串
- pDstStr-目的字符串(提取的值的存放位置)
- cIsolater-源字符串中字段的分隔符
- iDstStrSize-目的字符串的長度
- *輸出參數:無
- *返 回 值:TRUE-成功 FALSE-失敗
- *其它說明:無
- *修改日期 版本號 修改人 修改內容
- * --------------------------------------------------------------
- * 20151102 V1.0 Zhou Zhaoxiong 創建
- ***********************************************************************/
- BOOL GetValueFromStr(UINT16 iSerialNum, UINT8 iContentType, UINT8 *pSourceStr, UINT8 *pDstStr, UINT8 cIsolater, UINT32 iDstStrSize)
- {
- UINT8 *pStrBegin = NULL;
- UINT8 *pStrEnd = NULL;
- UINT8 szRetBuf[MAX_RET_BUF_LEN] = {0}; // 截取出的字符串放入該數組中
- UINT8 *pUINT8 = NULL;
- UINT16 *pUINT16 = NULL;
- UINT32 *pUINT32 = NULL;
- UINT32 iFieldLen = 0; // 用于表示每個字段的實際長度
- if (pSourceStr == NULL) // 對輸入指針的異常情況進行判斷
- {
- return FALSE;
- }
- //字段首
- pStrBegin = pSourceStr;
- while (--iSerialNum != 0)
- {
- pStrBegin = strchr(pStrBegin, cIsolater);
- if (pStrBegin == NULL)
- {
- return FALSE;
- }
- pStrBegin ++;
- }
- //字段尾
- pStrEnd = strchr(pStrBegin, cIsolater);
- if (pStrEnd == NULL)
- {
- return FALSE;
- }
- iFieldLen = (UINT16)(pStrEnd - pStrBegin);
- if(iFieldLen >= MAX_RET_BUF_LEN) //進行異常保護, 防止每個字段的值過長
- {
- iFieldLen = MAX_RET_BUF_LEN - 1;
- }
- memcpy(szRetBuf, pStrBegin, iFieldLen);
- //將需要的字段值放到pDstStr中去
- switch (iContentType)
- {
- case MML_STR_TYPE: //字符串類型
- {
- strncpy(pDstStr, szRetBuf, iDstStrSize);
- break;
- }
- case MML_INT8_TYPE: //字符類型
- {
- pUINT8 = (UINT8 *)pDstStr;
- *pDstStr = (UINT8)atoi(szRetBuf);
- break;
- }
- case MML_INT16_TYPE: // short int類型
- {
- pUINT16 = (UINT16 *)pDstStr;
- *pUINT16 = (UINT16)atoi(szRetBuf);
- break;
- }
- case MML_INT32_TYPE: // int類型
- {
- pUINT32 = (UINT32 *)pDstStr;
- *pUINT32 = (UINT32)atoi(szRetBuf);
- break;
- }
- default: // 一定要有default分支
- {
- return FALSE;
- }
- }
- return TRUE;
- }
- /**********************************************************************
- * 功能描述: 去掉字符串后面的回車換行符
- * 輸入參數: pszStr-輸入的字符串
- * 輸出參數: 無
- * 返 回 值: 無
- * 其它說明: 無
- * 修改日期 版本號 修改人 修改內容
- * ------------------------------------------------------------------
- * 20151102 V1.0 Zhou Zhaoxiong 創建
- ********************************************************************/
- void RemoveLineEnd(UINT8 *pszStr)
- {
- UINT32 iStrLen = 0;
- if (pszStr == NULL)
- {
- printf("RemoveLineEnd: pszStr is NULL!\n");
- return;
- }
- iStrLen = strlen(pszStr);
- while (iStrLen > 0)
- {
- if (pszStr[iStrLen-1] == '\n' || pszStr[iStrLen-1] == '\r')
- {
- pszStr[iStrLen-1] = '\0';
- }
- else
- {
- break;
- }
- iStrLen --;
- }
- return;
- }
- /**********************************************************************
- * 功能描述: 把內容寫到控制文件中
- * 輸入參數: pszContentLine-一條文件記錄
- * 輸出參數: 無
- * 返 回 值: 無
- * 其它說明: 無
- * 修改日期 版本號 修改人 修改內容
- * ------------------------------------------------------
- * 20151103 V1.0 Zhou Zhaoxiong 創建
- ***********************************************************************/
- void WriteToCtlFile(UINT8 *pszContentLine)
- {
- FILE *fpCtlFile = NULL;
- if (pszContentLine == NULL)
- {
- printf("WriteToCtlFile: pszContentLine is NULL.\n");
- return;
- }
- fpCtlFile = fopen(g_szCtlFileName, "w");
- if (fpCtlFile != NULL)
- {
- fputs(pszContentLine, fpCtlFile);
- fclose(fpCtlFile);
- fpCtlFile = NULL;
- printf("WriteToCtlFile: write ctl file successfully! file=%s, content=%s\n", g_szCtlFileName, pszContentLine);
- }
- else
- {
- printf("WriteToCtlFile: write ctl file failed! file=%s, content=%s\n", g_szCtlFileName, pszContentLine);
- }
- }
程序編譯及運行
將程序代碼上傳到Linux機器上,并在當前用戶的zhouzhaoxiong/zzx/CtlFileCreate/SrcFile目錄下上傳一些滿足命名規范的源文件,然后使用“gcc -g -o CtlFileCreate main.c CtlFileCreate.c”命令對程序進行編譯,生成“CtlFileCreate”文件;接著運行“CtlFileCreate”命令,可以看到在當前用戶的zhouzhaoxiong/zzx/CtlFileCreate/CtlFile目錄下有控制文件生成,在當前用戶的zhouzhaoxiong/zzx/CtlFileCreate/SrcFile_bak目錄下有源文件的備份文件生成。
查看控制文件內容,里面記錄的就是當前所處理的所有文件中的記錄總條數及所有記錄中的呼叫起始時間最小值和呼叫結束時間***值。
程序說明
***,為了便于說明,在本程序中,源文件的前綴是“Test_”,控制文件命名為“CtlFile.txt”。在實際的開發中,大家完全可以通過配置項來決定源文件及控制文件的命名規則。
第二,為了防止源文件被重復處理,當某個源文件處理完成之后,會被剪切到備份目錄中。這樣做也是為了方便之后校對控制文件中的內容。
第三,在讀取文件中的***條記錄時,將該條記錄中的呼叫起始時間和呼叫結束時間分別存放到兩個全局變量中,并按照格式寫控制文件;在讀取該文件中的其他記錄時,首先將該條記錄中的呼叫起始時間和呼叫結束時間與全局變量進行比較,確保全局變量中存放的是呼叫起始時間最小值和呼叫結束時間***值,記錄總條數加1,并將新的記錄內容寫入到控制文件中。
第四,在處理完當前目錄下的所有文件之后,程序會休眠一段時間,然后繼續掃描目錄。在實際的開發中,休眠間隔也是可以配置的。
(本文中的部分代碼已經提交到GitHub上,歡迎下載閱讀:
https://github.com/zhouzxi/CtlFileCreate)
【本文是51CTO專欄作者周兆熊的原創文章,作者微信公眾號:周氏邏輯(logiczhou)】