有段文件操作的代码,帮忙看看
CString m_strFilePath = "d:\\data.txt";
HANDLE hFile = CreateFile(m_strFilePath, GENERIC_WRITE,
FILE_SHARE_READ, NULL, CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL, NULL); if (hFile == INVALID_HANDLE_VALUE)
{
AfxMessageBox(_T("Could not create the file!"));
return;
}
CFile oriFile((int)hFile);
oriFile.SeekToBegin(); LPBYTE data = new BYTE[16]; for(int i=0;i<16;i++)
{
data[i] = 0xff;
}
oriFile.Write(data, 16); CloseHandle(hFile);
delete data;
但是,我用记事本打开看,里面没有任何东西。
CString m_strFilePath = "d:\\data.txt";
HANDLE hFile = CreateFile(m_strFilePath, GENERIC_WRITE,
FILE_SHARE_READ, NULL, CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL, NULL); if (hFile == INVALID_HANDLE_VALUE)
{
AfxMessageBox(_T("Could not create the file!"));
return;
}
CFile oriFile((int)hFile);
oriFile.SeekToBegin(); LPBYTE data = new BYTE[16]; for(int i=0;i<16;i++)
{
data[i] = 0xff;
}
oriFile.Write(data, 16); CloseHandle(hFile);
delete data;
但是,我用记事本打开看,里面没有任何东西。
但为什么记事本不行?我的是文本文件呀。
还有我用UltraEdit看也没有东西
HANDLE hFile = CreateFile(m_strFilePath, GENERIC_WRITE,
FILE_SHARE_READ, NULL, CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL, NULL); if (hFile == INVALID_HANDLE_VALUE)
{
AfxMessageBox(_T("Could not create the file!"));
return;
}
CFile oriFile((int)hFile);
oriFile.SeekToBegin(); LPBYTE data = new BYTE[16]; for(int i=0;i<16;i++)
{
data[i] = 0xff;
}
oriFile.Write(data, 16); //file close 了吗?
//file.Flush() 请给点时间给file 把缓冲写进文件
CloseHandle(hFile);
delete data;
//这里有内存泄露
//delete []data
你必成 0x31 看看