使用C库

  1. #include <stdio.h>
  2. #include <errno.h>
  3. #define BUFFER_SIZE 256
  4. int main(int argc,char* argv[]) {
  5. FILE* infile, * outfile;
  6. char rec[BUFFER_SIZE];
  7. size_t byteIn, byteOut;
  8. if (argc !=3)
  9. {
  10. printf("usage: cp file file2\n");
  11. return 1;
  12. }
  13. fopen_s(&infile,argv[1], "rb");
  14. if (infile == NULL)
  15. {
  16. perror(argv[1]);
  17. return 2;
  18. }
  19. fopen_s(&outfile,argv[2], "wb");
  20. if (outfile == NULL)
  21. {
  22. perror(argv[2]);
  23. return 3;
  24. }
  25. while ((byteIn = fread(rec,1,BUFFER_SIZE,infile))>0)
  26. {
  27. byteOut = fwrite(rec, 1, BUFFER_SIZE, outfile);
  28. if (byteOut != byteIn)
  29. {
  30. perror("fatal write error ");
  31. return 4;
  32. }
  33. }
  34. fclose(infile);
  35. fclose(outfile);
  36. return 0;
  37. }

使用windows 原生api

字符问题

  1. #include <stdio.h>
  2. #include <Windows.h>
  3. #define BUFFER_SIZE 256
  4. int main(int argc,LPTSTR argv[]) {
  5. HANDLE hIn, hOut;
  6. DWORD nIn, nOut;
  7. CHAR buffer[BUFFER_SIZE];
  8. if (argc < 3)
  9. {
  10. printf("usage: cp file file2\n");
  11. return 1;
  12. }
  13. hIn = CreateFile(argv[1], GENERIC_READ, FILE_SHARE_READ, NULL,
  14. OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
  15. if (hIn == INVALID_HANDLE_VALUE)
  16. {
  17. wprintf(argv[2]);
  18. printf("can not open file .error: %x\n", GetLastError());
  19. return 2;
  20. }
  21. hOut = CreateFile(argv[2], GENERIC_WRITE, 0, NULL,
  22. CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  23. if (hOut == INVALID_HANDLE_VALUE)
  24. {
  25. printf("can not open outfile.error: %x\n", GetLastError());
  26. return 3;
  27. }
  28. while (ReadFile(hIn,buffer,BUFFER_SIZE,&nIn,NULL) && nIn > 0)
  29. {
  30. WriteFile(hOut, buffer, BUFFER_SIZE, &nOut, NULL);
  31. if (nIn != nOut)
  32. {
  33. printf("fatal write error:%x\n", GetLastError());
  34. return 4;
  35. }
  36. }
  37. CloseHandle(hIn); CloseHandle(hOut);
  38. return 0;
  39. }


  1. #include <stdio.h>
  2. #include <Windows.h>
  3. #include <strsafe.h>
  4. int main(int argc,LPCWSTR argv[]) {
  5. WCHAR buffer[128];
  6. ::StringCchPrintf(buffer,_countof(buffer),L"this is my string from %u",::GetCurrentProcessId);
  7. ::MessageBox(nullptr,buffer,L"String Demo",MB_OK|MB_ICONINFORMATION);
  8. WCHAR path[MAX_PATH];
  9. ::GetSystemDirectory(path,_countof(path));
  10. printf("system directory : %ws\n", path);
  11. WCHAR computerName[MAX_COMPUTERNAME_LENGTH];
  12. DWORD len = _countof(computerName);
  13. if (!::GetComputerName(computerName, &len))
  14. {
  15. printf("computer name is : %ws (%u)\n", computerName,len);
  16. }
  17. SHELLEXECUTEINFO sei = { sizeof(sei) };
  18. sei.lpFile = L"c:\\windows\\win.ini";
  19. sei.lpVerb = L"open";
  20. sei.nShow = SW_SHOWNORMAL;
  21. ::ShellExecuteEx(&sei);
  22. return 0;
  23. }

获取windows 版本

  1. #include <Windows.h>
  2. #define BUILD_WINDOWS
  3. #include <stdio.h>
  4. #include <VersionHelpers.h>
  5. int main()
  6. {
  7. OSVERSIONINFO vi = { sizeof(vi) };
  8. ::GetVersionEx(&vi);
  9. printf("%u .%u .%u", vi.dwMajorVersion, vi.dwMinorVersion, vi.dwBuildNumber);
  10. return 0;
  11. }