In C ++ programming languages, file operations are a very important application technology. In this article, we will explain the application methods related to C ++ file copying in detail, so that we can get help in practical programming in the future.
Example of C ++ file copy code:
- # Include <iostream. h>
- # Include <afx. h>
- Void main ()
- {
- Char SourceName [81];
- Char DestinName [81];
- Cout <"\ n enter the source file name :";
- Cin> SourceName;
- Cout <"\ n enter the target file name :";
- Cin> DestinName;
- Try
- {
- CFile fileSource (SourceName, CFile: modeRead );
- CFile fileDestin (DestinName, CFile: modeCreate | CFile: modeWrite );
- Char c;
- While (fileSource. Read (& c, 1 ))
- FileDestin. Write (& c, 1 );
- }
- Catch (CFileException * e)
- {
- Switch (e-> m_cause)
- {
- Case CFileException: fileNotFound:
- Cout <"file not found! "<Endl;
- Break;
- Case CFileException: badPath:
- Cout <"path input error! "<Endl;
- Break;
- Case CFileException: accessDenied:
- Cout <"no access permission! "<Endl;
- Break;
- Case CFileException: diskFull:
- Cout <"Disk Full! "<Endl;
- Break;
- Default:
- Cout <"an unknown error occurred during file copy! "<Endl;
- Break;
- }
- }
- }
The preceding section describes how to copy C ++ files.