林永吉的Algorithm库
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

101 lignes
4.3 KiB

  1. // pch.cpp: 与预编译标头对应的源文件
  2. #include "pch.h"
  3. //***************************************************************************************************//
  4. //********读取ini文件函数,返回值为文件节的个数,入参为字符串数组的首地址,带回读取到的节名***************//
  5. //***************************************************************************************************//
  6. int CalcCount(int n, char(*str)[10],const char *FileName)
  7. {
  8. TCHAR chSectionNames[2048] = { 0 };//定义一个数组,保存读取节的名字
  9. char* pSectionName; //保存找到的某个节名字符串的首地址
  10. int i = 0; //i指向数组chSectionNames的某个位置,从0开始,顺序后移
  11. int j = 0; //j用来保存下一个节名字符串的首地址相对于当前i的位置偏移量
  12. int k = 0; //临时变量,用于给str数组赋值
  13. int m = 0; //临时变量,用于给str数组赋值
  14. int count = 0; //统计节的个数
  15. GetPrivateProfileSectionNames(chSectionNames, 2048, FileName);//获取文件中的节名,每个节名以"\0"分割,保存在chSectionNames中
  16. //要加头文件
  17. for (i = 0; i < 2048; i++, j++)//对chSectionName中保存的内容进行拆分
  18. {
  19. if (chSectionNames[0] == '\0')//如果第一个字符就是0,则说明ini中一个节也没有,
  20. {
  21. break;
  22. }
  23. if (chSectionNames[i] == '\0')
  24. {
  25. count++;
  26. pSectionName = &chSectionNames[i - j]; //找到一个0,则说明从这个字符往前,减掉j个偏移量,
  27. //就是一个节名的首地址
  28. j = -1; //找到一个节名后,j的值要还原,以统计下一个节名地址的偏移量
  29. //赋成-1是因为节名字符串的最后一个字符0是终止符,不能作为节名
  30. for (m = 0; m <= strlen(pSectionName); m++)//把找到的节名保存到str中
  31. {
  32. str[k][m] = *(pSectionName + m);
  33. }
  34. k++;//变量自加,用来保存下一个节名
  35. //在获取节名的时候可以获取该节中键的值,前提是我们知道该节中有哪些键。
  36. if (chSectionNames[i + 1] == 0)
  37. {
  38. break; //当两个相邻的字符都是0时,则所有的节名都已找到,循环终止
  39. }
  40. }
  41. }
  42. return count;//返回节名的个数
  43. }
  44. #if 1
  45. //***************************************************************************************************//
  46. //************切割字符串函数,返回值为int数组的首地址,通过传入int指针带回数组元素个数***************//
  47. //***************************************************************************************************//
  48. int* str_device(CString str, int* value_count)
  49. {
  50. char* token; //存放被切割后的第一个子串
  51. static int Section_devide[500];//存放字符切割完成以后的数组元素值
  52. char Section_value[500] = { 0 };//存放nums转换成string类型的结果
  53. memset(Section_value, 0, sizeof(char) * 500);
  54. *value_count = 0;
  55. strcpy(Section_value, str);//将CString类型的字符串转换成char类型,方便后面切割字符串
  56. //获得切割到的第一个字符串
  57. token = strtok(Section_value, ",");
  58. /* 继续获取其他的子字符串 */
  59. while (token != NULL) {
  60. Section_devide[*value_count] = (_ttoi)(token);//把切割得到的子串转为int,存到数组中去。
  61. token = strtok(NULL, ",");
  62. (*value_count)++; //记录存了多少个元素
  63. }
  64. return Section_devide; //返回数组首地址
  65. }
  66. #endif
  67. //字符串切割函数,返回值为切割后元素的个数,每个元素的类型为字符串型
  68. int str_device2(CString str, char(*return_str)[50])
  69. {
  70. int value_count = 0;
  71. char* token; //存放被切割后的第一个子串
  72. char Section_value[500] = { 0 };//存放nums转换成string类型的结果
  73. memset(Section_value, 0, sizeof(char) * 500);
  74. strcpy(Section_value, str);//将CString类型的字符串转换成char类型,方便后面切割字符串
  75. //获得切割到的第一个字符串
  76. token = strtok(Section_value, ",");
  77. /* 继续获取其他的子字符串 */
  78. while (token != NULL) {
  79. strcpy(return_str[value_count], token);
  80. token = strtok(NULL, ",");
  81. value_count++; //记录存了多少个元素
  82. }
  83. return value_count; //返回数组首地址
  84. }
  85. //字符串转bool
  86. bool CstrToBool(CString str)
  87. {
  88. if (str == "1") {
  89. return 1;
  90. }
  91. return 0;
  92. }