第一个C++程序

开始学习C++,用惯了java以后觉得这东西真是麻烦。。。
用unicode还挺麻烦的,了解了一下Windows API,学海无涯啊。

  1. #include <Windows.h>
  2. #include <string.h>
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. void ListFile(void);
  8.  
  9. int wmain(int argc, TCHAR* argv[]) {
  10.  
  11.     locale loc ("Chinese-simplified");
  12.     wcout.imbue(loc);
  13.  
  14.     if (argc > 1) {
  15.         SetCurrentDirectory(argv[1]);
  16.     }
  17.  
  18.  
  19.     ListFile();
  20.  
  21.     return 0;
  22. }
  23.  
  24. void ListFile(void) {
  25.     WIN32_FIND_DATA fd;
  26.     ZeroMemory(&fd, sizeof(WIN32_FIND_DATA));
  27.  
  28.     TCHAR currDir[255];
  29.     ZeroMemory(currDir, 255);
  30.    
  31.     GetCurrentDirectory(255, currDir);
  32.  
  33.     HANDLE hFile = FindFirstFile(L"*.*", &fd);
  34.  
  35.     if (hFile != INVALID_HANDLE_VALUE) {
  36.         do {
  37.             if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
  38.                     // This is a directory entry. Ignore the “.” and “..” entries.
  39.                     if (! (fd.cFileName[0] == '.' && (fd.cFileName[1] == 0 || fd.cFileName[1] == '.'))) {
  40.                         SetCurrentDirectory(fd.cFileName);
  41.                         wcout << "Directory changed to:" << currDir << "\\" << fd.cFileName << endl;
  42.                         ListFile();
  43.                         SetCurrentDirectory(currDir);
  44.                         wcout << "Directory changed to:" << currDir << endl;
  45.                     }
  46.             } else {
  47.                 wcout << fd.cFileName << endl;
  48.             }
  49.         } while (FindNextFile(hFile, &fd));
  50.     }
  51.     FindClose(hFile);
  52. }
标签:, , , , ,

相关日志

京ICP备05059555号