1
0

WinData.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // WinData.c
  2. #include "WinData.h"
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <windows.h>
  6. #define MAX_PATH_LENGTH 260
  7. char dirPath[MAX_PATH_LENGTH];
  8. char* GetUserDir(const char* dirName) {
  9. char userProfile[MAX_PATH_LENGTH];
  10. if (GetEnvironmentVariable("USERPROFILE", userProfile, MAX_PATH_LENGTH) == 0) {
  11. return NULL;
  12. }
  13. // 构建指定目录的完整路径
  14. snprintf(dirPath, sizeof(dirPath), "%s\\%s", userProfile, dirName);
  15. // 检查目录是否存在
  16. DWORD ftyp = GetFileAttributes(dirPath);
  17. if (ftyp == INVALID_FILE_ATTRIBUTES) {
  18. if (CreateDirectory(dirPath, NULL) ||
  19. GetLastError() == ERROR_ALREADY_EXISTS) {
  20. printf("目录 '%s' 创建成功或已经存在。\n", dirPath);
  21. } else {
  22. return NULL;
  23. }
  24. }
  25. return dirPath;
  26. }
  27. void saveWinData(const char *filename, const WinData *win) {
  28. char* dir = GetUserDir("lyvba");
  29. char bufile[MAX_PATH_LENGTH];
  30. sprintf(bufile, "%s\\%s", dir, filename);
  31. FILE *file = fopen(bufile, "wb");
  32. if (!file) {
  33. // perror("无法打开文件进行写入");
  34. return;
  35. }
  36. fwrite(win, sizeof(WinData), 1, file);
  37. fclose(file);
  38. }
  39. WinData loadWinData(const char *filename, WinData win) {
  40. char* dir = GetUserDir("lyvba");
  41. char bufile[MAX_PATH_LENGTH];
  42. sprintf(bufile, "%s\\%s", dir, filename);
  43. FILE *file = fopen(bufile, "rb");
  44. if (!file) {
  45. return win;
  46. }
  47. fread(&win, sizeof(WinData), 1, file);
  48. fclose(file);
  49. return win;
  50. }