CreateFile 打开文件重新写入失败,但GetlastError没有失败提示,

coFinder 2012-12-20 06:38:09
我有一颗双亲树,顺序数组存放,要保存到文件里面。每次树被更新之后就要写到文件里面去,

可是下面这个函数运行没有错误,但文件总是没有得到更新


int PtTree::SaveToFile(LPCTSTR Filename)
{
// ::DeleteFile(Filename);
//情况内容,
HANDLE hIndex = ::CreateFile(
Filename
,GENERIC_WRITE
,0
,NULL
,TRUNCATE_EXISTING
,FILE_ATTRIBUTE_NORMAL
,NULL);

//打开成功但就是没有把文件更新
if( INVALID_HANDLE_VALUE == hIndex )
{
ShowError();
return 0;
}
DWORD dwNumberOfBytes;

::SetFilePointer( hIndex, 0, 0 , FILE_BEGIN );

//写入节点个数
if(0 == WriteFile( hIndex, (LPVOID) &NodeCount, sizeof(NodeCount), &dwNumberOfBytes, NULL ) )
{
ShowError();
CloseHandle(hIndex);
return 0 ;
}

//写入叶子节点个数
if(0 == WriteFile( hIndex, (LPVOID)&LeafCount, sizeof(LeafCount), &dwNumberOfBytes, NULL ) )
{
ShowError();
CloseHandle(hIndex);
return 0 ;
}

//写入顺序树信息 ,这里在单步的时候可以看到dwNumberOfBytes为500,但文件的大小还是原来的248B
if(NodeCount && 0 == WriteFile( hIndex, (LPVOID)( &node), (NodeCount) * sizeof(PtNode), &dwNumberOfBytes, NULL ) )
{
ShowError();
CloseHandle(hIndex);
return 0 ;
}

//写入叶子数组
if(LeafCount && 0 == WriteFile( hIndex, (LPVOID)(&Leaf), (LeafCount) * sizeof(int), &dwNumberOfBytes, NULL ) )
{
ShowError();
CloseHandle(hIndex);
return 0 ;
}

CloseHandle(hIndex);

return 1;
}
...全文
203 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
科比布莱恩特 2012-12-21
  • 打赏
  • 举报
回复
引用 楼主 lilien1010 的回复:
我有一颗双亲树,顺序数组存放,要保存到文件里面。每次树被更新之后就要写到文件里面去, 可是下面这个函数运行没有错误,但文件总是没有得到更新 C/C++ code?1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545……
文件第二次不刷你可以重刷 一次。 很简单的返回一个操作的结果,判断是否返回成功即可。
赵4老师 2012-12-21
  • 打赏
  • 举报
回复
FlushFileBuffers The FlushFileBuffers function clears the buffers for the specified file and causes all buffered data to be written to the file. BOOL FlushFileBuffers( HANDLE hFile // open handle to file whose buffers are to be // flushed ); Parameters hFile An open file handle. The function flushes this file's buffers. The file handle must have GENERIC_WRITE access to the file. If hFile is a handle to a communications device, the function only flushes the transmit buffer. If hFile is a handle to the server end of a named pipe, the function does not return until the client has read all buffered data from the pipe. Windows NT: The function fails if hFile is a handle to console output. That is because console output is not buffered. The function returns FALSE, and GetLastError returns ERROR_INVALID_HANDLE. Windows 95: The function does nothing if hFile is a handle to console output. That is because console output is not buffered. The function returns TRUE, but it does nothing. Return Values If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To get extended error information, call GetLastError. Remarks The WriteFile and WriteFileEx functions typically write data to an internal buffer that the operating system writes to disk on a regular basis. The FlushFileBuffers function writes all of the buffered information for the specified file to disk. You can pass the same file handle used with the _lread, _lwrite, _lcreat, and related functions to FlushFileBuffers. QuickInfo Windows NT: Requires version 3.1 or later. Windows: Requires Windows 95 or later. Windows CE: Unsupported. Header: Declared in winbase.h. Import Library: Use kernel32.lib. See Also File I/O Overview, File Functions, _lread, _lwrite, _lcreat, WriteFile, WriteFileEx
作者:k0shl ####一些环境说明: 编译环境:Windows 10 x64 build 1607 项目IDE:VS2013 测试环境:Windows 7 x86、Windows 10 x86 build 1607 参数介绍: "-l" :开启日志记录模式(不会影响主日志记录模块) "-s" :驱动枚举模块 "-d" :打开设备驱动的名称 "-i" :待Fuzz的ioctl code,默认从0xnnnn0000-0xnnnnffff "-n" :在探测阶段采用null pointer模式,该模式下极易fuzz 到空指针引用漏洞,不加则常规探测模式 "-r" :指定明确的ioctl code范围 "-u" :只fuzz -i参数给定的ioctl code "-f" :在探测阶段采用0x00填充缓冲区 "-q" :在Fuzz阶段不显示填充input buffer的数据内容 "-e" :在探测和fuzz阶段打印错误信息(如getlasterror()) "-h" :帮助信息 ####常用Fuzz命令实例: kDriver Fuzz.exe -s 进行驱动枚举,将CreateFile成功的驱动设备名称,以及部分受限的驱动设备名称打印并写入Enum Driver.txt文件中 kDriver Fuzz.exe -d X -i 0xaabb0000 -f -l 对X驱动的ioctl code 0xaabb0000-0xaabbffff范围进行探测及对可用的ioctl code进行fuzz,探测时除了正常探测外增加0x00填充缓冲区探测,开启数据日志记录(如增加-u参数,则只对ioctl code 0xaabb0000探测,若是有效ioctl code则进入fuzz阶段) kDriver Fuzz.exe -d X -r 0xaabb1122-0xaabb3344 -n -l 对X驱动的ioctl code 0xaabb1122-0xaabb3344范围内进行探测,探测时采用null pointer模式,并数据日志记录

24,854

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧