我想通过将元数据编码到备用数据流来跟踪对文件执行某项操作的次数。到目前为止,我已经覆盖了OnSaveDocument 在保存时将此值写入文件:
BOOL CEmergenceDoc::OnSaveDocument(LPCTSTR lpszPathName)
{
CRichEditDoc::OnSaveDocument(lpszPathName);
CString stream = lpszPathName;
stream += ":iterations";
LPCTSTR newPath = stream;
HANDLE hStream;
DWORD dwRet;
hStream = CreateFile(newPath, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, 0,
NULL);
if(hStream == INVALID_HANDLE_VALUE )
printf( "Cannot open testfile:stream\n" );
else {
CString str;
CEmergenceView * current = CEmergenceView::GetView();
str.Format(_T("%d"), current->iterations);
WriteFile(hStream, str, str.GetLength(), &dwRet, NULL);
}
return TRUE;
}
我还重写了OnFileOpen来读取保存文件的备用数据流。
void CEmergenceApp::OnFileOpen()
{
HANDLE hStream;
DWORD dwRet;
char ReadBuffer[BUFFERSIZE] = {0};
CWinAppEx::OnFileOpen();
OutputDebugString((CString)"here");
CEmergenceDoc * currentDoc = CEmergenceDoc::GetDoc();
CString currentPath = currentDoc->GetPathName();
currentPath += ":iterations";
OutputDebugString(currentPath);
hStream = CreateFile( currentPath,
GENERIC_WRITE,
FILE_SHARE_WRITE,
NULL,
OPEN_ALWAYS,
OPEN_EXISTING,
NULL );
if( hStream == INVALID_HANDLE_VALUE )
printf( "Cannot open testfile:stream\n" );
else {
ReadFile(hStream, ReadBuffer, BUFFERSIZE-1, &dwRet, NULL);
OutputDebugString((CString)"here");
}
CString test;
test = "";
test += ReadBuffer;
OutputDebugString(test);
}
从我的代码中可以看出,我正在尝试通过打印来测试这是否正常工作 ReadBuffer中的任何内容,但是当打开文件时,即使它确实进入ReadFile,也不会打印出任何内容。有人可以看看这个并告诉我有什么问题吗?谢谢。
编辑:
我在cmd中执行了以下命令:
更多< testfile的:流
并且它似乎正在将数据写入备用数据流....所以问题必须在于阅读它.......
答案 0 :(得分:0)
一些意见:
a)您似乎没有在任何一个函数中关闭流句柄hStream
b)检查WriteFile()
和ReadFile()
来电
c)在CEmergenceApp::OnFileOpen()
中,使用GENERIC_READ
或GENERIC_READ | GENERIC_WRITE