Linux under the control of of statistics file generated by C code implementation

  • 2020-05-07 20:11:54
  • OfStack

This article shares the C code implementation case generated by Linux control (statistics) file for your reference, the specific content is as follows

1. Requirements description
randomly places a file in a directory under Linux machine. The file contains user number, start and end time and other fields.

For example, there are two files Test_1.txt and Test_2.txt in this directory, where the Test_1.txt file contents are:

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|

In other words, the format of each record in the file is: call number |, call number |, call start time, |, call end time, |, and the generated control file CtlFile is required. The content of txt is:

20151101 110909|20160109 091545|5|

That is, the minimum starting time of the five records in Test_1.txt and Test_2.txt files is "20151101 110909", and the maximum ending time is "2016010091545". So far, a total of five records have been processed. In other words, the format of the control file is: the minimum call start time | call end time maximum | record the total number of |.

2. Program code
This program 1 includes three code files: main.c, CtlFileCreate.c and CtlFileCreate.h. The specific code is as follows:

main.c


/**********************************************************************
*  All rights reserved  (C)2016, Zhou Zhaoxiong . 
*
*  File name: CtlFileCreate.c
*  File id: none 
*  Content summary: the directory file reading and control the file generation 
*  Other instructions: none 
*  Current version: V1.0
*  Completion date: 20160109
*
**********************************************************************/
#include "CtlFileCreate.h"

/**********************************************************************
*  Function description: main function 
*  Input parameter: none 
*  Output parameter: none 
*  return   Back to the   Value: no 
*  Other instructions: none 
*  Modification date      The version number     The modifier        Modify the content 
* -------------------------------------------------------------------
* 20160109    V1.0   Zhou Zhaoxiong     create 
***********************************************************************/

INT32 main()
{
  ReadCtlFile();  //  Gets the control file storage path, the full path name of the control file, and the file content field values 

  ReadSrcFileAndWriteCtlFile();  //  Scan the source file directory ,  And write the control file 

  return 0;
}

CtlFileCreate.h


/**********************************************************************
*  All rights reserved  (C)2015, Zhou Zhaoxiong . 
*
*  File name: CtlFileCreate.h
*  File id: none 
*  Content summary: the directory file reading and control the file generation 
*  Other instructions: none 
*  Current version: V1.0
*  Completion date: 20151102
*
**********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>

//  Data type redefinition 
typedef unsigned char    UINT8;
typedef unsigned short int UINT16;
typedef unsigned int    UINT32;
typedef signed  int    INT32;
typedef unsigned char    BOOL;

//  The parameter types 
#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

//  Maximum field length 
#define MAX_RET_BUF_LEN   1024

//  Source file field structure 
typedef struct
{
  UINT8 szSrcNumber[50];
  UINT8 szDstNumber[50];
  UINT8 szDataStartTime[50]; 
  UINT8 szDataEndTime[50]; 
} T_SrcFileContent;

//  Function declaration 
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


/**********************************************************************
*  All rights reserved  (C)2015, Zhou Zhaoxiong . 
*
*  File name: CtlFileCreate.c
*  File id: none 
*  Content summary: the directory file reading and control the file generation 
*  Other instructions: none 
*  Current version: V1.0
*  Completion date: 20151102
*
**********************************************************************/
#include "CtlFileCreate.h"

//  The global variable 
UINT8 g_szSourceDir[500]  = {0};     //  The source directory to scan 
UINT8 g_szCtlFileDir[500]  = {0};     //  The directory where the generated control files are stored 
UINT8 g_szSourceBakDir[500] = {0};     //  Backup directory of source files after processing 
UINT8 g_szCtlFileName[256] = {0};     //  Controls the full path name of the file 
UINT8 g_szDataStartTime[50] = {0};     //  The earliest start time for data records in all source files 
UINT8 g_szDataEndTime[50]  = {0};     //  The latest end time of data records in all source files 
UINT32 g_iRecordsSum     = 0;      //  The total number of records processed 

/**********************************************************************
*  Function description:   Read the start time, end time, and number of record bars in the control file 
*  Input parameters:   There is no 
*  Output parameters:   There is no 
*  return   Back to the   Value:   There is no 
*  Other notes:   There is no 
*  Modification date       The version number      The modifier         Modify the content 
* ------------------------------------------------------------------
* 20151102     V1.0   Zhou Zhaoxiong       create 
********************************************************************/
void ReadCtlFile(void)
{
  UINT8 *pszHomePath = NULL; 
  FILE *fpCtlFile  = NULL;
  UINT8 szBuf[500] = {0}; 

  //  Read the start time, end time, and number of record bars in the control file ,  If it is the same day the program restart ,  The number of records continues to be numbered 
  pszHomePath = getenv("HOME");
  if (pszHomePath == NULL)
  {
    return;
  }

  snprintf(g_szCtlFileDir, sizeof(g_szCtlFileDir)-1, "%s/zhouzhaoxiong/zzx/CtlFileCreate/CtlFile", pszHomePath); //  Control file storage directory 

  snprintf(g_szCtlFileName, sizeof(g_szCtlFileName)-1, "%s/CtlFile.txt", g_szCtlFileDir); //  Controls the full path name of the file 

  fpCtlFile = fopen(g_szCtlFileName, "r");
  if (fpCtlFile != NULL)
  {
    fgets(szBuf, sizeof(szBuf), fpCtlFile);

    //  Get start time 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;
    }

    //  Get end time 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;
    }

    //  Gets the number of record bars 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);
  }
}


/**********************************************************************
*  Function description:   Scan the source file directory ,  And write the control file 
*  Input parameters:   There is no 
*  Output parameters:   There is no 
*  return   Back to the   Value:   There is no 
*  Other notes:   There is no 
*  Modification date       The version number      The modifier         Modify the content 
* ------------------------------------------------------------------
* 20151102     V1.0   Zhou Zhaoxiong       create 
********************************************************************/
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);   //  Source files are stored in directories 

  snprintf(g_szSourceBakDir, sizeof(g_szSourceBakDir)-1, "%s/zhouzhaoxiong/zzx/CtlFileCreate/SrcFile_bak", pszHomePath); //  Backup directory of source files 

  while (1)
  {  
    pDir = opendir(g_szSourceDir);
    if (NULL == pDir)
    {
      printf("ReadSrcFileAndWriteCtlFile: pDir is NULL!\n");
      continue;
    }

    while ((pDirent = readdir(pDir)) != NULL)  //  Scan source directory ,  Get file name 
    {
      if (strncmp(pDirent->d_name, "Test_", strlen("Test_")) == 0)   //  If the prefix of the source file is matched ,  The contents of the file are read and the control file is written 
      {
        memset(szSrcFile, 0x00, sizeof(szSrcFile));
        snprintf(szSrcFile, sizeof(szSrcFile)-1, "%s/%s", g_szSourceDir, pDirent->d_name, g_szSourceBakDir);
        GetSrcFileContentAndWriteCtlFile(szSrcFile);   //  Gets the contents of the source file ,  And write the control file 

        //  After processing ,  Cut the file to the backup directory 
        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);  //  every 1 Minutes to scan 1 time 
  }
}


/**********************************************************************
*  Function description:   Gets the contents of the source file ,  And write the control file 
*  Input parameters:  pszSrcFileName- Source file name with path 
*  Output parameters:   There is no 
*  return   Back to the   Value:   There is no 
*  Other notes:   There is no 
*  Modification date       The version number      The modifier         Modify the content 
* ------------------------------------------------------------------
* 20151102     V1.0   Zhou Zhaoxiong       create 
********************************************************************/
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) //  Read-only mode open 
  {
    printf("GetSrcFileContentAndWriteCtlFile: open src file failed!\n");
    return; 
  }
  else
  {
    while (feof(fp) == 0 && ferror(fp) == 0)
    {
      //  Each row corresponds to 1 A source file record 
      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); //  Remove the carriage return line after the string 

      if (strlen(szContentLine) == 0)  //  If it's a blank row ,  I'm going to go ahead and do it 1 article 
      {
        printf("GetSrcFileContentAndWriteCtlFile: the length of ContentLine is 0, continue.\n"); 
        continue;
      }

      GetSrcFileFieldValue(szContentLine, &tSrcFileContent);  //  To obtain 1 The value of each field in the record 

      memset(szContentLine, 0x00, sizeof(szContentLine));
      GetCtlFileContentAndWrite(&tSrcFileContent, szContentLine); //  Assembles the contents written to the control file 

      WriteToCtlFile(szContentLine);  //  Write to the control file 
    }

    fclose(fp);
    fp = NULL;
  }
}


/**********************************************************************
*  Function description:   Assembles the contents written to the control file 
*  Input parameters:  ptSrcFileContent- In the source file 1 The value of each field in the record 
*  Output parameters:  pszContentBuffer- The cache that holds the content 
*  return   Back to the   Value:   There is no 
*  Other notes:   The control file is recorded as : DataStartTime|DataEndTime|RecordsSum|
*  Modification date       The version number      The modifier         Modify the content 
* ------------------------------------------------------------------
* 20151102     V1.0   Zhou Zhaoxiong       create 
********************************************************************/
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;
  }

  //  Pairs according to the size of the value g_szDataStartTime For the assignment 
  if (strlen(g_szDataStartTime) == 0)  //  On the day of the first 1 article 
  {
    strncpy(g_szDataStartTime, ptSrcFileContent->szDataStartTime, strlen(ptSrcFileContent->szDataStartTime));
  }
  else
  {
    if (strncmp(g_szDataStartTime, ptSrcFileContent->szDataStartTime, strlen(ptSrcFileContent->szDataStartTime)) > 0) //  Change to minimum time 
    {
      memset(g_szDataStartTime, 0x00, sizeof(g_szDataStartTime));

      strncpy(g_szDataStartTime, ptSrcFileContent->szDataStartTime, strlen(ptSrcFileContent->szDataStartTime));
    }
  }

  //  Pairs according to the size of the value g_szDataEndTime For the assignment 
  if (strlen(g_szDataEndTime) == 0)  //  On the day of the first 1 article 
  {
    strncpy(g_szDataEndTime, ptSrcFileContent->szDataEndTime, strlen(ptSrcFileContent->szDataEndTime));
  }
  else
  {
    if (strncmp(g_szDataEndTime, ptSrcFileContent->szDataEndTime, strlen(ptSrcFileContent->szDataEndTime)) < 0) //  Change to maximum time 
    {
      memset(g_szDataEndTime, 0x00, sizeof(g_szDataEndTime));

      strncpy(g_szDataEndTime, ptSrcFileContent->szDataEndTime, strlen(ptSrcFileContent->szDataEndTime));
    }
  }

  //  Record total number of entries plus 1
  g_iRecordsSum = g_iRecordsSum + 1;   //  The total number of entries added to all records for the day 1

  //  print 3 The contents of the fields 
  printf("GetCtlFileContentAndWrite: DataStartTime is %s, DataEndTime is %s, RecordsSum is %d\n", g_szDataStartTime, g_szDataEndTime, g_iRecordsSum);

  //  Assembles the message content written to the control file 
  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));
}


/**********************************************************************
*  Function description:   Gets the values of the individual fields in the source file 
*  Input parameters:  pszContentLine-1 records 
*  Output parameters:  ptSrcFileContent- In the source file 1 The value of each field in the record 
*  return   Back to the   Value:   There is no 
*  Other notes:   The format of each record in the source file is : SrcNumber|DstNumber|DataStartTime|DataEndTime|
*  Modification date       The version number      The modifier         Modify the content 
* ------------------------------------------------------------------
* 20151102     V1.0   Zhou Zhaoxiong       create 
********************************************************************/
void GetSrcFileFieldValue(UINT8 *pszContentLine, T_SrcFileContent *ptSrcFileContent)
{
  if (pszContentLine == NULL || ptSrcFileContent == NULL)
  {
    printf("GetSrcFileFieldValue: ContentLine or SrcFileContent is NULL!\n");
    return;
  }

  //  Get source number 
  if (TRUE != GetValueFromStr(1, MML_STR_TYPE, pszContentLine, ptSrcFileContent->szSrcNumber, '|', sizeof(ptSrcFileContent->szSrcNumber)))
  {
    printf("GetSrcFileFieldValue: exec GetValueFromStr to get szSrcNumber failed!\n");
    return;
  }

  //  Get destination number 
  if (TRUE != GetValueFromStr(2, MML_STR_TYPE, pszContentLine, ptSrcFileContent->szDstNumber, '|', sizeof(ptSrcFileContent->szDstNumber)))
  {
    printf("GetSrcFileFieldValue: exec GetValueFromStr to get szDstNumber failed!\n");
    return;
  }

  //  Get start time 
  if (TRUE != GetValueFromStr(3, MML_STR_TYPE, pszContentLine, ptSrcFileContent->szDataStartTime, '|', sizeof(ptSrcFileContent->szDataStartTime)))
  {
    printf("GetSrcFileFieldValue: exec GetValueFromStr to get szDataStartTime failed!\n");
    return;
  }

  //  Get end time 
  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);


}


/**********************************************************************
*  Function description:   Application of dormancy 
*  Input parameters:  iCountMs- Sleep time ( unit :ms)
*  Output parameters:   There is no 
*  return   Back to the   Value:   There is no 
*  Other notes:   There is no 
*  Modification date       The version number      The modifier         Modify the content 
* ------------------------------------------------------------------
* 20151102     V1.0   Zhou Zhaoxiong       create 
********************************************************************/
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);  //  call select Function blocker 
}


/**********************************************************************
* Function description: gets something in a string 1 The value of 
* Input parameters: iSerialNum- Field Numbers ( As a positive integer )
      iContentType- The type of content you need to get 
      pSourceStr- The source string 
      pDstStr- Destination string ( The location where the extracted values are stored )
      cIsolater- The separator of a field in the source string 
      iDstStrSize- The length of the destination string 
* Output parameter: none 
* return   Back to the   Value: TRUE- successful  FALSE- failure 
* Other instructions: none 
* Modification date      The version number        The modifier       Modify the content 
* --------------------------------------------------------------
* 20151102    V1.0     Zhou Zhaoxiong     create 
***********************************************************************/
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};   //  The intercepted string is put into the array 
  UINT8 *pUINT8          = NULL;
  UINT16 *pUINT16          = NULL;
  UINT32 *pUINT32          = NULL;
  UINT32 iFieldLen         = 0;   //  Used to represent the actual length of each field 

  if (pSourceStr == NULL)           //  To determine the exception of the input pointer 
  {
    return FALSE;
  }
  // The first field 
  pStrBegin = pSourceStr;
  while (--iSerialNum != 0)
  {
    pStrBegin = strchr(pStrBegin, cIsolater);
    if (pStrBegin == NULL)
    {
      return FALSE;
    }
    pStrBegin ++;
  }

  // Field in the tail 
  pStrEnd = strchr(pStrBegin, cIsolater);
  if (pStrEnd == NULL)
  {
    return FALSE;
  }

  iFieldLen = (UINT16)(pStrEnd - pStrBegin);
  if(iFieldLen >= MAX_RET_BUF_LEN) // Abnormal protection ,  Prevents the value of each field from being too long 
  {
    iFieldLen = MAX_RET_BUF_LEN - 1;
  }

  memcpy(szRetBuf, pStrBegin, iFieldLen);

  // Place the required field values pDstStr in 
  switch (iContentType)
  {
    case MML_STR_TYPE:            // String type 
    {
      strncpy(pDstStr, szRetBuf, iDstStrSize);
      break;
    }

    case MML_INT8_TYPE:            // Character types 
    {
      pUINT8  = (UINT8 *)pDstStr;
      *pDstStr = (UINT8)atoi(szRetBuf);
      break;
    }

    case MML_INT16_TYPE:           // short int type 
    {
      pUINT16 = (UINT16 *)pDstStr;
      *pUINT16 = (UINT16)atoi(szRetBuf);
      break;
    }

    case MML_INT32_TYPE:           // int type 
    {
      pUINT32 = (UINT32 *)pDstStr;
      *pUINT32 = (UINT32)atoi(szRetBuf);
      break;
    }

    default:                 // 1 Need to have a default branch 
    {
      return FALSE;
    }
  }

  return TRUE;
}


/**********************************************************************
*  Function description:   Remove the carriage return line after the string 
*  Input parameters:  pszStr- Input string 
*  Output parameters:   There is no 
*  return   Back to the   Value:   There is no 
*  Other notes:   There is no 
*  Modification date       The version number      The modifier         Modify the content 
* ------------------------------------------------------------------
* 20151102     V1.0   Zhou Zhaoxiong       create 
********************************************************************/
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;
}


/**********************************************************************
 *  Function description:   Write to the control file 
 *  Input parameters:  pszContentLine-1 Strip file record 
 *  Output parameters:   There is no 
 *  return   Back to the   Value:   There is no 
 *  Other notes:   There is no 
 *  Modification date      The version number     The modifier        Modify the content 
 * ------------------------------------------------------
 * 20151103    V1.0   Zhou Zhaoxiong     create 
 ***********************************************************************/
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);
  }
}

3. Compile and run
Upload code to Linux machine, and in the current user zhouzhaoxiong/zzx CtlFileCreate/SrcFile directory upload 1 meet the naming conventions of the source files, and then use the "gcc g - o CtlFileCreate main. c CtlFileCreate. c" command to compile the program, generate "CtlFileCreate" files; Then run a "CtlFileCreate" command, you can see in the current user zhouzhaoxiong/zzx CtlFileCreate/CtlFile directory control files are generated, in the current user zhouzhaoxiong/zzx CtlFileCreate/SrcFile_bak directory active file backup files are generated.

View the contents of the control file, which records the total number of records in all the files being processed, the minimum call start time and the maximum call end time in all records.

4. Program description
no. 1, for illustration purposes, in this program, the source file is prefixed with "Test_", and the control file is named "CtlFile.txt". In real development, you can use configuration items to determine the naming of source and control files.

Second, to prevent source files from being processed repeatedly, when a source file is processed, it is clipped to the backup directory. This is also done to facilitate later proofreading and control of the contents of the file.

3. When the first record in the file is read, the call start time and call end time in the record are stored in two global variables respectively, and the control file is written in accordance with the format. When reading the other records in the file, which is first recorded in the call start time and end of the call time compared with global variables, ensure global variable to store the call start time minimum and maximum time end of the call, record the total number of article 1, written to control the content of the new record file.

Fourth, after processing all the files in the current directory, the program will sleep for a period of time, and then continue to scan the directory. In real development, sleep intervals can also be configured.

The above is the entire content of this article, I hope to help you with your study.


Related articles: