VC::FileOpen & FileClose & getCurrentTime
星期三, 03月 8th, 2006void CT0202Dlg::OnFileOpen()
{
CFileDialog openFileDialog(1);
openFileDialog.DoModal();
m_file_path = openFileDialog.GetPathName();
HANDLE hFileHandle;
hFileHandle = CreateFile(m_file_path, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if( hFileHandle == INVALID_HANDLE_VALUE ){
AfxMessageBox("Open Failed ");
return;
}
char readContent[1025];
DWORD readLength;
m_edit.Empty();
do {
ReadFile( hFileHandle, readContent, 1024, &readLength, NULL );
readContent[readLength] = 0;
m_edit = m_edit + (CString)readContent;
} while ( readLength == 1024 );
UpdateData(FALSE);
CloseHandle( hFileHandle );
}
void CT0202Dlg::OnFileSave()
{
CFileDialog openFileDialog(0);
openFileDialog.DoModal();
m_file_path = openFileDialog.GetPathName();
HANDLE hFileHandle;
hFileHandle = CreateFile(m_file_path, GENERIC_WRITE, FILE_SHARE_WRITE, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if( hFileHandle == INVALID_HANDLE_VALUE ){
AfxMessageBox("Create File Failed ");
return;
}
UpdateData( FALSE );
char writeContent[1025];
DWORD writeLength;
DWORD nNumberOfByteToWrite = m_edit.GetLength();
for( int i = 0; i < nNumberOfByteToWrite; ) {
for( int j = 0; j < 1024 && i < nNumberOfByteToWrite; i++, j++ )
writeContent[j] = m_edit.GetAt(i);
writeContent[j] = 0;
WriteFile( hFileHandle, writeContent, j, &writeLength, NULL );
}
CloseHandle( hFileHandle );
}
GetCurrentTime
COleDateTime dateTime;
dateTime = COleDateTime::GetCurrentTime();
int y = dateTime.GetYear();
int m = dateTime.GetMonth();
int d = dateTime.GetDay();