C ++ file copy code

Source: Internet
Author: User
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
  1. # Pragma once
  2. # Include <string>
  3. # Include "file. H"
  4. Using namespace STD;
  5. Namespace common
  6. {
  7. Namespace File
  8. {
  9. Class syncfolder: Public common: file: fileclass
  10. {
  11. PRIVATE:
  12. Wstring m_sourcedirectory;
  13. Wstring m_targetdirectory;
  14. Const wstring * m_ext;
  15. Const bool * m_reverseext;
  16. Protected:
  17. Void filemethod (const wstring &);
  18. Public:
  19. Syncfolder (const wstring & sourcedirectory, const wstring & targetdirectory, const wstring & Ext, bool reverseext): fileclass (),
  20. M_sourcedirectory (common: file: getsecurepath (sourcedirectory), m_targetdirectory (common: file: getsecurepath (targetdirectory ))
  21. {
  22. M_ext = new wstring (EXT );
  23. M_reverseext = new bool (reverseext );
  24. }
  25. ~ Syncfolder (void)
  26. {
  27. Delete m_ext;
  28. Delete m_reverseext;
  29. }
  30. Void exec ();
  31. };
  32. }
  33. }

. Cpp File

  1. # Include "stdafx. H"
  2. # Include "syncfolder. H"
  3. # Include "file. H"
  4. Void common: file: syncfolder: exec ()
  5. {
  6. Readfiles (m_sourcedirectory, * m_ext );
  7. }
  8. Void common: file: syncfolder: filemethod (const wstring & filename)
  9. {
  10. // Obtain the relative path
  11. Wstring offsetpath =
  12. Common: file: getrelativepath (m_sourcedirectory,
  13. Common: file: getpathfromfilepath (filename ));
  14. // Obtain the new path
  15. Wstring targetfilepath;
  16. If (offsetpath. Empty ())
  17. Targetfilepath = m_targetdirectory;
  18. Else
  19. Targetfilepath = m_targetdirectory + L "//" + offsetpath;
  20. // Create a folder
  21. Common: file: createpath (targetfilepath );
  22. // Create a new file path
  23. Wstring newfilename = targetfilepath + L "//" + common: file: getfilenamefromfilepath (filename );
  24. // Copy an object
  25. Copyfile (filename. c_str (), newfilename. c_str (), true );
  26. }

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.