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)容為:
- 15696192591|15696192592|20151103 120909|20151103 201545|
- 15696192593|15696192594|20151103 110909|20151103 191545|
- 02344273522|02344273523|20160108 110909|20160109 091545|
Test_2.txt文件內(nèi)容為:
- 15696192595|15696192596|20151102 120909|20151104 201545|
- 15696192597|15696192598|20151101 110909|20151103 191545|
即文件中的每條記錄的格式為:呼叫號(hào)碼|被呼叫號(hào)碼|呼叫起始時(shí)間|呼叫結(jié)束時(shí)間|,要求生成的控制文件CtlFile.txt的內(nèi)容為:
- 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
- /**********************************************************************
- * 版權(quán)所有 (C)2016, Zhou Zhaoxiong。
- *
- * 文件名稱(chēng):CtlFileCreate.c
- * 文件標(biāo)識(shí):無(wú)
- * 內(nèi)容摘要:目錄中文件的讀取及控制文件的生成
- * 其它說(shuō)明:無(wú)
- * 當(dāng)前版本:V1.0
- * 作 者:Zhou Zhaoxiong
- * 完成日期:20160109
- *
- **********************************************************************/
- #include "CtlFileCreate.h"
- /**********************************************************************
- * 功能描述:主函數(shù)
- * 輸入?yún)?shù):無(wú)
- * 輸出參數(shù):無(wú)
- * 返 回 值:無(wú)
- * 其它說(shuō)明:無(wú)
- * 修改日期 版本號(hào) 修改人 修改內(nèi)容
- * -------------------------------------------------------------------
- * 20160109 V1.0 Zhou Zhaoxiong 創(chuàng)建
- ***********************************************************************/
- INT32 main()
- {
- ReadCtlFile(); // 獲取控制文件存放路徑、控制文件全路徑名及文件內(nèi)容字段值
- ReadSrcFileAndWriteCtlFile(); // 掃描源文件目錄, 并寫(xiě)控制文件
- return 0;
- }
CtlFileCreate.h
- /**********************************************************************
- * 版權(quán)所有 (C)2015, Zhou Zhaoxiong。
- *
- * 文件名稱(chēng):CtlFileCreate.h
- * 文件標(biāo)識(shí):無(wú)
- * 內(nèi)容摘要:目錄中文件的讀取及控制文件的生成
- * 其它說(shuō)明:無(wú)
- * 當(dāng)前版本:V1.0
- * 作 者:Zhou Zhaoxiong
- * 完成日期:20151102
- *
- **********************************************************************/
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <dirent.h>
- // 數(shù)據(jù)類(lèi)型重定義
- typedef unsigned char UINT8;
- typedef unsigned short int UINT16;
- typedef unsigned int UINT32;
- typedef signed int INT32;
- typedef unsigned char BOOL;
- // 參數(shù)類(lèi)型
- #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
- // 字段***長(zhǎng)度
- #define MAX_RET_BUF_LEN 1024
- // 源文件字段結(jié)構(gòu)體
- typedef struct
- {
- UINT8 szSrcNumber[50];
- UINT8 szDstNumber[50];
- UINT8 szDataStartTime[50];
- UINT8 szDataEndTime[50];
- } T_SrcFileContent;
- // 函數(shù)聲明
- 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
- /**********************************************************************
- * 版權(quán)所有 (C)2015, Zhou Zhaoxiong。
- *
- * 文件名稱(chēng):CtlFileCreate.c
- * 文件標(biāo)識(shí):無(wú)
- * 內(nèi)容摘要:目錄中文件的讀取及控制文件的生成
- * 其它說(shuō)明:無(wú)
- * 當(dāng)前版本: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}; // 所有源文件中數(shù)據(jù)記錄的最早開(kāi)始時(shí)間
- UINT8 g_szDataEndTime[50] = {0}; // 所有源文件中數(shù)據(jù)記錄的最晚結(jié)束時(shí)間
- UINT32 g_iRecordsSum = 0; // 已處理的記錄的總條數(shù)
- /**********************************************************************
- * 功能描述: 讀取控制文件中的開(kāi)始時(shí)間、結(jié)束時(shí)間和記錄條數(shù)
- * 輸入?yún)?shù): 無(wú)
- * 輸出參數(shù): 無(wú)
- * 返 回 值: 無(wú)
- * 其它說(shuō)明: 無(wú)
- * 修改日期 版本號(hào) 修改人 修改內(nèi)容
- * ------------------------------------------------------------------
- * 20151102 V1.0 Zhou Zhaoxiong 創(chuàng)建
- ********************************************************************/
- void ReadCtlFile(void)
- {
- UINT8 *pszHomePath = NULL;
- FILE *fpCtlFile = NULL;
- UINT8 szBuf[500] = {0};
- // 讀取控制文件中的開(kāi)始時(shí)間、結(jié)束時(shí)間和記錄條數(shù), 如果是當(dāng)天程序重啟, 則記錄條數(shù)繼續(xù)編號(hào)
- 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);
- // 獲取開(kāi)始時(shí)間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;
- }
- // 獲取結(jié)束時(shí)間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;
- }
- // 獲取記錄條數(shù)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);
- }
- }
- /**********************************************************************
- * 功能描述: 掃描源文件目錄, 并寫(xiě)控制文件
- * 輸入?yún)?shù): 無(wú)
- * 輸出參數(shù): 無(wú)
- * 返 回 值: 無(wú)
- * 其它說(shuō)明: 無(wú)
- * 修改日期 版本號(hào) 修改人 修改內(nèi)容
- * ------------------------------------------------------------------
- * 20151102 V1.0 Zhou Zhaoxiong 創(chuàng)建
- ********************************************************************/
- 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) // 如果匹配上了源文件的前綴, 則讀取文件內(nèi)容并寫(xiě)控制文件
- {
- memset(szSrcFile, 0x00, sizeof(szSrcFile));
- snprintf(szSrcFile, sizeof(szSrcFile)-1, "%s/%s", g_szSourceDir, pDirent->d_name, g_szSourceBakDir);
- GetSrcFileContentAndWriteCtlFile(szSrcFile); // 獲取源文件中的內(nèi)容, 并寫(xiě)控制文件
- // 處理完成之后, 將文件剪切到備份目錄中
- 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分鐘掃描一次
- }
- }
- /**********************************************************************
- * 功能描述: 獲取源文件中的內(nèi)容, 并寫(xiě)控制文件
- * 輸入?yún)?shù): pszSrcFileName-帶路徑的源文件名
- * 輸出參數(shù): 無(wú)
- * 返 回 值: 無(wú)
- * 其它說(shuō)明: 無(wú)
- * 修改日期 版本號(hào) 修改人 修改內(nèi)容
- * ------------------------------------------------------------------
- * 20151102 V1.0 Zhou Zhaoxiong 創(chuàng)建
- ********************************************************************/
- 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) // 只讀方式打開(kāi)
- {
- printf("GetSrcFileContentAndWriteCtlFile: open src file failed!\n");
- return;
- }
- else
- {
- while (feof(fp) == 0 && ferror(fp) == 0)
- {
- // 每行對(duì)應(yīng)一條源文件記錄
- 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); // 去掉字符串后面的回車(chē)換行符
- if (strlen(szContentLine) == 0) // 如果為空行, 則繼續(xù)處理下一條
- {
- printf("GetSrcFileContentAndWriteCtlFile: the length of ContentLine is 0, continue.\n");
- continue;
- }
- GetSrcFileFieldValue(szContentLine, &tSrcFileContent); // 獲取一條記錄中各個(gè)字段的值
- memset(szContentLine, 0x00, sizeof(szContentLine));
- GetCtlFileContentAndWrite(&tSrcFileContent, szContentLine); // 組裝寫(xiě)入控制文件中的內(nèi)容
- WriteToCtlFile(szContentLine); // 將內(nèi)容寫(xiě)到控制文件中
- }
- fclose(fp);
- fp = NULL;
- }
- }
- /**********************************************************************
- * 功能描述: 組裝寫(xiě)入控制文件中的內(nèi)容
- * 輸入?yún)?shù): ptSrcFileContent-源文件中一條記錄中各個(gè)字段的值
- * 輸出參數(shù): pszContentBuffer-存放內(nèi)容的緩存
- * 返 回 值: 無(wú)
- * 其它說(shuō)明: 控制文件中記錄為: DataStartTime|DataEndTime|RecordsSum|
- * 修改日期 版本號(hào) 修改人 修改內(nèi)容
- * ------------------------------------------------------------------
- * 20151102 V1.0 Zhou Zhaoxiong 創(chuàng)建
- ********************************************************************/
- 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;
- }
- // 根據(jù)值的大小對(duì)g_szDataStartTime進(jìn)行賦值
- if (strlen(g_szDataStartTime) == 0) // 當(dāng)天***條
- {
- strncpy(g_szDataStartTime, ptSrcFileContent->szDataStartTime, strlen(ptSrcFileContent->szDataStartTime));
- }
- else
- {
- if (strncmp(g_szDataStartTime, ptSrcFileContent->szDataStartTime, strlen(ptSrcFileContent->szDataStartTime)) > 0) // 修改成最小時(shí)間
- {
- memset(g_szDataStartTime, 0x00, sizeof(g_szDataStartTime));
- strncpy(g_szDataStartTime, ptSrcFileContent->szDataStartTime, strlen(ptSrcFileContent->szDataStartTime));
- }
- }
- // 根據(jù)值的大小對(duì)g_szDataEndTime進(jìn)行賦值
- if (strlen(g_szDataEndTime) == 0) // 當(dāng)天***條
- {
- strncpy(g_szDataEndTime, ptSrcFileContent->szDataEndTime, strlen(ptSrcFileContent->szDataEndTime));
- }
- else
- {
- if (strncmp(g_szDataEndTime, ptSrcFileContent->szDataEndTime, strlen(ptSrcFileContent->szDataEndTime)) < 0) // 修改成***時(shí)間
- {
- memset(g_szDataEndTime, 0x00, sizeof(g_szDataEndTime));
- strncpy(g_szDataEndTime, ptSrcFileContent->szDataEndTime, strlen(ptSrcFileContent->szDataEndTime));
- }
- }
- // 記錄總條數(shù)加1
- g_iRecordsSumg_iRecordsSum = g_iRecordsSum + 1; // 當(dāng)天所有記錄的總條數(shù)加1
- // 打印三個(gè)字段的內(nèi)容
- printf("GetCtlFileContentAndWrite: DataStartTime is %s, DataEndTime is %s, RecordsSum is %d\n", g_szDataStartTime, g_szDataEndTime, g_iRecordsSum);
- // 組裝寫(xiě)到控制文件中的消息內(nèi)容
- 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));
- }
- /**********************************************************************
- * 功能描述: 獲取源文件中的各個(gè)字段的值
- * 輸入?yún)?shù): pszContentLine-一條記錄
- * 輸出參數(shù): ptSrcFileContent-源文件中一條記錄中各個(gè)字段的值
- * 返 回 值: 無(wú)
- * 其它說(shuō)明: 源文件中每條記錄的格式為: SrcNumber|DstNumber|DataStartTime|DataEndTime|
- * 修改日期 版本號(hào) 修改人 修改內(nèi)容
- * ------------------------------------------------------------------
- * 20151102 V1.0 Zhou Zhaoxiong 創(chuàng)建
- ********************************************************************/
- void GetSrcFileFieldValue(UINT8 *pszContentLine, T_SrcFileContent *ptSrcFileContent)
- {
- if (pszContentLine == NULL || ptSrcFileContent == NULL)
- {
- printf("GetSrcFileFieldValue: ContentLine or SrcFileContent is NULL!\n");
- return;
- }
- // 獲取源號(hào)碼
- if (TRUE != GetValueFromStr(1, MML_STR_TYPE, pszContentLine, ptSrcFileContent->szSrcNumber, '|', sizeof(ptSrcFileContent->szSrcNumber)))
- {
- printf("GetSrcFileFieldValue: exec GetValueFromStr to get szSrcNumber failed!\n");
- return;
- }
- // 獲取目的號(hào)碼
- if (TRUE != GetValueFromStr(2, MML_STR_TYPE, pszContentLine, ptSrcFileContent->szDstNumber, '|', sizeof(ptSrcFileContent->szDstNumber)))
- {
- printf("GetSrcFileFieldValue: exec GetValueFromStr to get szDstNumber failed!\n");
- return;
- }
- // 獲取開(kāi)始時(shí)間
- if (TRUE != GetValueFromStr(3, MML_STR_TYPE, pszContentLine, ptSrcFileContent->szDataStartTime, '|', sizeof(ptSrcFileContent->szDataStartTime)))
- {
- printf("GetSrcFileFieldValue: exec GetValueFromStr to get szDataStartTime failed!\n");
- return;
- }
- // 獲取結(jié)束時(shí)間
- 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);
- }
- /**********************************************************************
- * 功能描述: 程序休眠
- * 輸入?yún)?shù): iCountMs-休眠時(shí)間(單位:ms)
- * 輸出參數(shù): 無(wú)
- * 返 回 值: 無(wú)
- * 其它說(shuō)明: 無(wú)
- * 修改日期 版本號(hào) 修改人 修改內(nèi)容
- * ------------------------------------------------------------------
- * 20151102 V1.0 Zhou Zhaoxiong 創(chuàng)建
- ********************************************************************/
- 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); // 調(diào)用select函數(shù)阻塞程序
- }
- /**********************************************************************
- *功能描述:獲取字符串中某一個(gè)字段的值
- *輸入?yún)?shù):iSerialNum-字段編號(hào)(為正整數(shù))
- iContentType-需要獲取的內(nèi)容的類(lèi)型
- pSourceStr-源字符串
- pDstStr-目的字符串(提取的值的存放位置)
- cIsolater-源字符串中字段的分隔符
- iDstStrSize-目的字符串的長(zhǎng)度
- *輸出參數(shù):無(wú)
- *返 回 值:TRUE-成功 FALSE-失敗
- *其它說(shuō)明:無(wú)
- *修改日期 版本號(hào) 修改人 修改內(nèi)容
- * --------------------------------------------------------------
- * 20151102 V1.0 Zhou Zhaoxiong 創(chuàng)建
- ***********************************************************************/
- 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}; // 截取出的字符串放入該數(shù)組中
- UINT8 *pUINT8 = NULL;
- UINT16 *pUINT16 = NULL;
- UINT32 *pUINT32 = NULL;
- UINT32 iFieldLen = 0; // 用于表示每個(gè)字段的實(shí)際長(zhǎng)度
- if (pSourceStr == NULL) // 對(duì)輸入指針的異常情況進(jìn)行判斷
- {
- 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) //進(jìn)行異常保護(hù), 防止每個(gè)字段的值過(guò)長(zhǎng)
- {
- iFieldLen = MAX_RET_BUF_LEN - 1;
- }
- memcpy(szRetBuf, pStrBegin, iFieldLen);
- //將需要的字段值放到pDstStr中去
- switch (iContentType)
- {
- case MML_STR_TYPE: //字符串類(lèi)型
- {
- strncpy(pDstStr, szRetBuf, iDstStrSize);
- break;
- }
- case MML_INT8_TYPE: //字符類(lèi)型
- {
- pUINT8 = (UINT8 *)pDstStr;
- *pDstStr = (UINT8)atoi(szRetBuf);
- break;
- }
- case MML_INT16_TYPE: // short int類(lèi)型
- {
- pUINT16 = (UINT16 *)pDstStr;
- *pUINT16 = (UINT16)atoi(szRetBuf);
- break;
- }
- case MML_INT32_TYPE: // int類(lèi)型
- {
- pUINT32 = (UINT32 *)pDstStr;
- *pUINT32 = (UINT32)atoi(szRetBuf);
- break;
- }
- default: // 一定要有default分支
- {
- return FALSE;
- }
- }
- return TRUE;
- }
- /**********************************************************************
- * 功能描述: 去掉字符串后面的回車(chē)換行符
- * 輸入?yún)?shù): pszStr-輸入的字符串
- * 輸出參數(shù): 無(wú)
- * 返 回 值: 無(wú)
- * 其它說(shuō)明: 無(wú)
- * 修改日期 版本號(hào) 修改人 修改內(nèi)容
- * ------------------------------------------------------------------
- * 20151102 V1.0 Zhou Zhaoxiong 創(chuàng)建
- ********************************************************************/
- 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;
- }
- /**********************************************************************
- * 功能描述: 把內(nèi)容寫(xiě)到控制文件中
- * 輸入?yún)?shù): pszContentLine-一條文件記錄
- * 輸出參數(shù): 無(wú)
- * 返 回 值: 無(wú)
- * 其它說(shuō)明: 無(wú)
- * 修改日期 版本號(hào) 修改人 修改內(nèi)容
- * ------------------------------------------------------
- * 20151103 V1.0 Zhou Zhaoxiong 創(chuàng)建
- ***********************************************************************/
- 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);
- }
- }
程序編譯及運(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)】