Using C ++ to copy files is much more difficult than I thought, and it is more interesting to directly operate windows APIs than using C # Only file classes and directory classes. This process makes me realize not only the API, but also the essence of programming. First, I would like to share with you my code. I would also like to ask your c ++ predecessors for advice .. H file
- # Pragma once
- # Include <string>
- # Include "file. H"
- Using namespace STD;
- Namespace common
- {
- Namespace File
- {
- Class syncfolder: Public common: file: fileclass
- {
- PRIVATE:
- Wstring m_sourcedirectory;
- Wstring m_targetdirectory;
- Const wstring * m_ext;
- Const bool * m_reverseext;
- Protected:
- Void filemethod (const wstring &);
- Public:
- Syncfolder (const wstring & sourcedirectory, const wstring & targetdirectory, const wstring & Ext, bool reverseext): fileclass (),
- M_sourcedirectory (common: file: getsecurepath (sourcedirectory), m_targetdirectory (common: file: getsecurepath (targetdirectory ))
- {
- M_ext = new wstring (EXT );
- M_reverseext = new bool (reverseext );
- }
- ~ Syncfolder (void)
- {
- Delete m_ext;
- Delete m_reverseext;
- }
- Void exec ();
- };
- }
- }
. Cpp File
- # Include "stdafx. H"
- # Include "syncfolder. H"
- # Include "file. H"
- Void common: file: syncfolder: exec ()
- {
- Readfiles (m_sourcedirectory, * m_ext );
- }
- Void common: file: syncfolder: filemethod (const wstring & filename)
- {
- // Obtain the relative path
- Wstring offsetpath =
- Common: file: getrelativepath (m_sourcedirectory,
- Common: file: getpathfromfilepath (filename ));
- // Obtain the new path
- Wstring targetfilepath;
- If (offsetpath. Empty ())
- Targetfilepath = m_targetdirectory;
- Else
- Targetfilepath = m_targetdirectory + L "//" + offsetpath;
- // Create a folder
- Common: file: createpath (targetfilepath );
- // Create a new file path
- Wstring newfilename = targetfilepath + L "//" + common: file: getfilenamefromfilepath (filename );
- // Copy an object
- Copyfile (filename. c_str (), newfilename. c_str (), true );
- }
In fact, in a previous blog, I wrote a function to traverse the file directory (Folder traversal code C ++ (Win32 platform). In the parameter list of this function, there is a function pointer. My intention is to use this function pointer to process the objects traversed. In C #, you can use delegate to declare a type similar to the function pointer, and then define an object to point to the address of the execution method, so I also want to do the same in C ++. However, the C ++ compiler tells me that this writing method is incorrect. In C ++, the function pointer points to a static address. If I want to direct the pointer to a member method of the class instance, this is not allowed in C ++ (I heard a solution in boost ). This makes me understand that the similarities in C ++ and C # are different. However, this gives me the space to imagine: How the C # compiler uses delegate to implement function pointer functions or the essential differences between them. Another important experience is that C ++'s development environment is far less intelligent than C # (vs2008), especially smart sensing. This made me always feel unstable when I started some code, like a drunk walking and shaking. Later, I found that this concern stems from my lack of full understanding of the type check feature of the C ++ compiler. We can rest assured that we can write code and then compile. If there is a problem, the compiler will tell you exactly. This recognition is helpful for refactoring the code. In subsequent work, I feel very comfortable Refactoring for those C # code, because the compiler will tell me where the problem is. That is to say, when writing C # code, try to avoid using data types such as datatable, because the compiler cannot check the data types, in this way, we will not be able to enjoy the benefits of the compiler type check feature. There is also the way unit testing works. At the beginning, I wrote C ++ code. I am not sure whether the output result is correct or what it looks like when I write a line of code. To solve this problem, I basically write special debugging code for each function. Later, I found that this is a good way to work with high efficiency. This is a unit test in the. NET world. In short, I have benefited a lot from C ++ learning. I hope to have more exchanges with you.