第一个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. }
标签:, , , , ,

相关日志 随机文章

Comments

6 Responses to “第一个C++程序”

  1. qatang on 2007-04-12 4:37 pm

    贱人,开始写c++了??

  2. feuvan on 2007-04-12 5:08 pm

  3. winson.tang on 2007-04-15 1:55 pm

    牛,第一个c++程序就这么牛

  4. sirius on 2007-04-15 2:31 pm

    怎么着就cpp了,拿个c程序你忽悠谁啊?

  5. bianbian on 2007-04-15 4:16 pm

    嗯,你太有菜了

  6. 挖 on 2007-04-16 7:36 pm

    cpp的不要

Leave a Reply




请输入验证码

Use "<coolcode></coolcode>" to publish your code.

Line breaks and paragraphs are automatically converted.

Please keep comments relevant. Off-topic, offensive or inappropriate comments may be edited or removed.

京ICP备05059555号