|
-
March 9th, 2015, 08:40 AM
#1
File opening and closing after 24 hours
hello,
We are using a file operation for logging the data, file should be closed after the 24 hrs from the time the file is opened and open the new file for the current date.
Issue:
How to close the file after 24 hrs and create the new file ,please look into the code and provide me if any solution or suggest.
Code:
//Header files
#include<time.h>
#include<string.h>
#include<stdio.h>
#include<windows.h>
#define LG_UPS_BB 100
#define LG_TCU_TEMP 200
// Global variables.
FILE *g_dlLogFile ; //!< File pointer to store the data logging file handler.
CRITICAL_SECTION g_dlCriticalSection ; //!< Critical section object.
int d_dlLastSNo = 0 ;
HANDLE d_hdlDataLog ;
DWORD d_tidDataLog ;
int d_upBattery = 20 ;
int d_tcuTemp = 24 ;
// Functions
void dlHandler(void *pParam) ;
int dlInitialize(void)
{
time_t u32Time ; // Variable to store the current time.
char strFileName[50] ; // File name to log the data.
struct tm stTime ; // Structure name to store the time.
///Step1: Initialize a critical section object.
InitializeCriticalSection(&g_dlCriticalSection) ;
///Step2: Log file initialized to NULL.
g_dlLogFile = NULL ;
///Step3: Reading system current time.
u32Time = time(NULL) ;
stTime = *(localtime(&u32Time)) ;
///Step4: Generating the log file name.
sprintf(strFileName, "%d%02d%02d_s%02d", (stTime.tm_year + 1900), (stTime.tm_mon + 1), stTime.tm_mday, d_dlLastSNo) ;
///Step5: Open the log file for data logging.
g_dlLogFile = fopen(strFileName, "w") ;
if (g_dlLogFile == NULL)
{
printf ("\nDL: File not created , errno = %d", GetLastError()) ;
}
///Step7: Creating the thread.
d_hdlDataLog = CreateThread(NULL, 0, (void*)dlHandler, 0, 0, &d_tidDataLog) ;
if(d_hdlDataLog == NULL)
{
printf ("\nDL: Thread creation failed, errno = %d", GetLastError()) ;
}
///Step8: Return the status.
return 0;
}
int dlSetLogID(int pId)
{
BOOL bStatus = -1 ; // Hold the status for PostThreadMessage.
///Step1: Wait for the ownership of the critical section object.
EnterCriticalSection(&g_dlCriticalSection) ;
///Step2: Send the log id to the buffer.
bStatus = PostThreadMessageA(d_tidDataLog, (WM_USER + pId), 0, 0) ;
///Step3: Report error in message posted and return FALSE.
if (bStatus == FALSE)
{
printf ("\nDL: Message is not posted in the buffer, errno = %d", GetLastError()) ;
}
///Step4: Release the ownership of the critical section object.
LeaveCriticalSection(&g_dlCriticalSection) ;
///Step5: Return the status.
return 0;
}
void dlHandler(void *pParam)
{
MSG stMsg ; // Pointer to the message structure
BOOL bStatus = FALSE ; // bStatus to hold the return value for PostMessage.
unsigned int iLogNo = 0 ; // For serial numbers in the log file.
time_t u32Time ; // To store the current time.
struct tm stTime ; // Structure name to store the time.
char strFileName[20] ;
if(g_dlLogFile == NULL)
{
#ifdef DEBUG_DATA_LOG
printf ("\nDL: Log file pointer is invalid, errno = %d", errno) ;
#endif
}
// Loop to log the record.
while(1)
{
// Wait until the message is received.
WaitMessage() ;
///Step1: Retrieves the message from the buffer.
bStatus = GetMessageA(&stMsg, NULL, 0, 0) ;
if(bStatus == -1)
{
printf ("\nDL: Message is not retrieved from the buffer, errno = %d", GetLastError()) ;
}
else
{
///Step4: Get the system time.
u32Time = time(NULL) ;
stTime = *(localtime(&u32Time) );
// Increment the Log Numbers for each record.
iLogNo++ ;
///Step5: Store the log no, system time, mode of operation and log id in the file.
fprintf(g_dlLogFile,"\n%d,%d:%d:%d,%d", iLogNo, stTime.tm_hour, stTime.tm_min,
stTime.tm_sec, (stMsg.message-WM_USER)) ;
///Step6: For storing the data in the file.
switch((stMsg.message - WM_USER))
{
case LG_UPS_BB:
{
//Battery backup value stored in the file.
fprintf(g_dlLogFile,",%u", d_upBattery);
break;
}
case LG_TCU_TEMP:
{
//Temperature value is stored in the file.
fprintf(g_dlLogFile,",%.02f", d_tcuTemp);
break;
}
default:
break;
}
}
Sleep(100) ;
}
}
int dlClose()
{
///Step1: Close the Log file pointer.
if(g_dlLogFile != NULL)
fclose(g_dlLogFile) ;
///Step2: Delete the critical section object.
DeleteCriticalSection(&g_dlCriticalSection) ;
///Step3: Close the thread handle.
if(d_hdlDataLog != NULL)
CloseHandle(d_hdlDataLog) ;
///Step4: Return the status.
return 0;
}
int main ()
{
dlInitialize(void) ;
dlSetLogID(LG_UPS_BB) ;
return 0 ;
}
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|