gpt4 book ai didi

c++ - 刷新不调用文件更改

转载 作者:太空宇宙 更新时间:2023-11-04 11:48:54 26 4
gpt4 key购买 nike

我正在编写一个程序,其中一个进程 A 读取另一个进程 B 附加到文件的数据。我正在使用 ReadDirectoryChangesW 进行通知。问题是在我关闭 B 中的句柄之前不会生成通知,尽管我正在刷新使用 fflush 将内容写入文件。代码如下所示

文件编写器:

int _tmain(int argc, _TCHAR* argv[])
{
FILE *fp;

fp=_fsopen("log.txt", "a", _SH_DENYNO);

char str[4096];

for(int i=1;i<4096;i++)
str[i]=i;

while(true){
fwrite(str,1,4096,fp);
fflush(fp);
Sleep(2000);
}

return 0;
}

文件阅读器:

#include "stdafx.h"
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <tchar.h>
#include <conio.h>
#include <assert.h>
#include <share.h>

void _tmain(int argc, TCHAR *argv[])
{
FILE *fp;
fp=_fsopen("C:\\Users\\dell\\Documents\\Visual Studio 2012\\Projects\\FileWriter\\FileWriter\\log.txt", "r", _SH_DENYNO);
int last_size=0,new_size=0;

if(fp==NULL)
return ;

HANDLE m_hMonitoredDir = CreateFile(TEXT("C:\\Users\\dell\\Documents\\Visual Studio 2012\\Projects\\FileWriter\\FileWriter"), FILE_LIST_DIRECTORY,
FILE_SHARE_DELETE|FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL, OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS, NULL );

if ( m_hMonitoredDir == INVALID_HANDLE_VALUE )
{
DWORD dwErr = GetLastError();
printf("error");
return;
}

char szBuf[ MAX_PATH ];
DWORD dwBytesRead = 0;
int flag=0;
char *buffer;

while ( ReadDirectoryChangesW( m_hMonitoredDir, szBuf, MAX_PATH, FALSE, FILE_NOTIFY_CHANGE_LAST_WRITE,&dwBytesRead, NULL, NULL ))
{
PFILE_NOTIFY_INFORMATION pstFileNotif = (PFILE_NOTIFY_INFORMATION)( szBuf );
if ( pstFileNotif->Action == FILE_ACTION_MODIFIED )
{
char szNotifFilename[ MAX_PATH ] = { 0 };
if ( int iNotifFilenameLen = WideCharToMultiByte( CP_OEMCP, NULL,
pstFileNotif->FileName,
pstFileNotif->FileNameLength / sizeof( WCHAR ),
szNotifFilename, sizeof( szNotifFilename ) / sizeof( char ),
NULL, NULL ) )
{

if ( strcmp("log.txt", szNotifFilename ) == 0 )
{
fseek(fp, 0, SEEK_END);
new_size = ftell(fp);
fseek(fp,last_size,SEEK_SET);
int size=new_size-last_size;
buffer=new char[size+1];
fread(buffer,1,size,fp);
buffer[size]='\0';
printf("%s",buffer);
free(buffer);
}
}
}
}

}

谁能帮我在 B 中使用 fflush 后立即收到通知?

最佳答案

我认为这是不可能的。根据the documentationFILE_NOTIFY_CHANGE_LAST_WRITE 上(强调我的):

Any change to the last write-time of files in the watched directory or subtree causes a change notification wait operation to return. The operating system detects a change to the last write-time only when the file is written to the disk. For operating systems that use extensive caching, detection occurs only when the cache is sufficiently flushed.

fflush()确保文件数据传回操作系统,但不保证数据写入磁盘,因为通常涉及大量缓存:

Buffers are normally maintained by the operating system, which determines the optimal time to write the data automatically to disk: when a buffer is full, when a stream is closed, or when a program terminates normally without closing the stream. The commit-to-disk feature of the run-time library lets you ensure that critical data is written directly to disk rather than to the operating-system buffers.

正如其他人在评论中所说,您最好使用命名管道来实现您的目标,因为您只处理一个已知文件。

关于c++ - 刷新不调用文件更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19039087/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com