⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 copyfile.cpp

📁 < VC++编程宝典> 编程必备,适合初学者!
💻 CPP
字号:
BOOL CFileDemoView::CopyFile( const char *lpSrcFilename,
	const char *lpDestFilename, int nBuffersize )
{
	CFile Out, In;
	int nFilesize;
	char *lpBuffer;

	// Attempt to open the input file.
	if( !In.Open( lpSrcFilename, CFile::modeRead ) ){
		AfxMessageBox( "Could not open the input file." );
		return( FALSE );
		}

	// Attempt to create the output file.
	if( !Out.Open( lpDestFilename,
		CFile::modeWrite | CFile::modeCreate ) ){
		AfxMessageBox( "Could not open the output file." );
		return( FALSE );
		}

	// Create the copy buffer.
	lpBuffer = new char [nBuffersize];
	if( lpBuffer == NULL ){
		AfxMessageBox( "Could not allocate the copy buffer." );
		return( FALSE );
		}

	// Get the input file status so that
	// we can set the output file's status
	// to the same. This will preserve
	// things such as time and date.
	CFileStatus rStatus;
	In.GetStatus( lpSrcFilename, rStatus );

	// Get file file size so that we know
	// when we're done copying.
	nFilesize = In.GetLength();

	while( nFilesize > 0 ){

		// Start off by assuming
		// we'll read in enough bytes
		// to fill the buffer.
		int nSize = nBuffersize;

		// If the number of bytes remaining
		// isn't enough to fill the
		// buffer, adjust the size.
		if( nSize > nFilesize )
			nSize = nFilesize;

		// Read in the bytes and make
		// sure we catch any exceptions
		// that are thrown.
		try{
			In.Read( lpBuffer, nSize );
			}
		catch( CFileException *e ){

			// Format a message from
			// the system.
			char *lpMsgBuf;
			if( FormatMessage(
				FORMAT_MESSAGE_ALLOCATE_BUFFER |
				FORMAT_MESSAGE_FROM_SYSTEM,
				NULL, e->m_lOsError,
				MAKELANGID( LANG_NEUTRAL,
					SUBLANG_DEFAULT ),
				(LPSTR) &lpMsgBuf, 0, NULL ) > 0 ){
				AfxMessageBox( lpMsgBuf );
				LocalFree( lpMsgBuf );
				}

			// Free the exception and
			// return.
			e->Delete();
			return( FALSE );
			}

		// Write out the bytes and make
		// sure we catch any exceptions
		// that are thrown.
		try{
			Out.Write( lpBuffer, nSize );
			}

		catch( CFileException *e ){

			// Format a message from
			// the system.
			char *lpMsgBuf;
			if( FormatMessage(
				FORMAT_MESSAGE_ALLOCATE_BUFFER |
				FORMAT_MESSAGE_FROM_SYSTEM,
				NULL, e->m_lOsError,
				MAKELANGID( LANG_NEUTRAL,
					SUBLANG_DEFAULT ),
				(LPSTR) &lpMsgBuf, 0, NULL ) > 0 ){
				AfxMessageBox( lpMsgBuf );
				LocalFree( lpMsgBuf );
				}

			// Free the exception and
			// return.
			e->Delete();
			return( FALSE );
			}

		nFilesize -= nSize;

		}

	// Close the output file so that the
	// SetStatus() function won't fail.
	Out.Close();
	CFile::SetStatus( lpDestFilename, rStatus );

	// Delete the buffer.
	delete [] lpBuffer;

	return( TRUE );

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -