linux c 监控文件内容改变,如何让程序监视C ++中的文件修改?

WinCE的一个工作例子void FileInfoHelper::WatchFileChanges( TCHAR *ptcFileBaseDir, TCHAR *ptcFileName ){static int iCount = 0;DWORD dwWaitStatus; HANDLE dwChangeHandles; if( ! ptcFileBaseDir || ! ptcFileName ) return;wstring wszFileNameToWatch = ptcFileName;dwChangeHandles = FindFirstChangeNotification(

ptcFileBaseDir,

FALSE,

FILE_NOTIFY_CHANGE_FILE_NAME |

FILE_NOTIFY_CHANGE_DIR_NAME |

FILE_NOTIFY_CHANGE_ATTRIBUTES |

FILE_NOTIFY_CHANGE_SIZE |

FILE_NOTIFY_CHANGE_LAST_WRITE |

FILE_NOTIFY_CHANGE_LAST_ACCESS |

FILE_NOTIFY_CHANGE_CREATION |

FILE_NOTIFY_CHANGE_SECURITY |

FILE_NOTIFY_CHANGE_CEGETINFO    );if (dwChangeHandles == INVALID_HANDLE_VALUE) {

printf("\n ERROR: FindFirstChangeNotification function failed [%d].\n", GetLastError());

return;}while (TRUE) {

// Wait for notification.

printf("\n\n[%d] Waiting for notification...\n", iCount);

iCount++;

dwWaitStatus = WaitForSingleObject(dwChangeHandles, INFINITE);

switch (dwWaitStatus)

{

case WAIT_OBJECT_0:

printf( "Change detected\n" );

DWORD iBytesReturned, iBytesAvaible;

if( CeGetFileNotificationInfo( dwChangeHandles, 0, NULL, 0, &iBytesReturned, &iBytesAvaible) != 0 )

{

std::vector vecBuffer( iBytesAvaible );

if( CeGetFileNotificationInfo( dwChangeHandles, 0, &vecBuffer.front(), vecBuffer.size(), &iBytesReturned, &iBytesAvaible) != 0 ) {

BYTE* p_bCurrent = &vecBuffer.front();

PFILE_NOTIFY_INFORMATION info = NULL;

do {

info = reinterpret_cast( p_bCurrent );

p_bCurrent += info->NextEntryOffset;

if( wszFileNameToWatch.compare( info->FileName ) == 0 )

{

wcout <FileName <Action;

switch(info->Action) {

case FILE_ACTION_ADDED:

break;

case FILE_ACTION_MODIFIED:

break;

case FILE_ACTION_REMOVED:

break;

case FILE_ACTION_RENAMED_NEW_NAME:

break;

case FILE_ACTION_RENAMED_OLD_NAME:

break;

}

}

}while (info->NextEntryOffset != 0);

}

}

if ( FindNextChangeNotification( dwChangeHandles ) == FALSE )

{

printf("\n ERROR: FindNextChangeNotification function failed [%d].\n", GetLastError());

return;

}

break;

case WAIT_TIMEOUT:

printf("\nNo changes in the timeout period.\n");

break;

default:

printf("\n ERROR: Unhandled dwWaitStatus [%d].\n", GetLastError());

return;

break;

}}FindCloseChangeNotification( dwChangeHandles );}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值