一个较完整的开发实例

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define nullptr NULL
  5. // 去除首尾空格
  6. int strRemoveSpace(char* distination, char* source)
  7. {
  8. if (distination == nullptr || source == nullptr)
  9. {
  10. return -1;
  11. }
  12. char* p1 = source;
  13. char* p2 = source + strlen(source) - 1;
  14. // 1、查找source字符串前置空格,找到就跳过,最终确定头指针位置(空格结束)
  15. while (*p1 == ' ')
  16. {
  17. p1++;
  18. }
  19. // 2、按照上面的方法处理,最终确定尾指针的位置
  20. while (*p2 == ' ')
  21. {
  22. p2--;
  23. }
  24. // 3、把值赋给 distination
  25. while (p1 <= p2)
  26. {
  27. *distination = *p1;
  28. distination++;
  29. p1++;
  30. }
  31. return 0;
  32. }
  33. //////////////////////////////////////////////////////////////////////////
  34. // 做一个键值对读取的函数,根据key读取value
  35. // key = "value"
  36. //
  37. int getKeybyValue(char* pKeyValude, char* pKey, char* pValude)
  38. {
  39. char rv = 0;
  40. char* p = nullptr;
  41. //char buf[1024];
  42. //////////////////////////////////////////////////////////////////////////
  43. // 这种写法不是太详细,如果环境复杂,应该采用下面那种写法
  44. if (pKeyValude == nullptr || pKey == nullptr || pValude == nullptr)
  45. {
  46. rv = -1;
  47. printf("func getKeybyValue() err:%d pKeyValude == nullptr || pKey == nullptr || pValude == nullptr", rv);
  48. return rv;
  49. }
  50. //////////////////////////////////////////////////////////////////////////
  51. // 在环境复杂的情况下,应该采用这种写法
  52. if (pKeyValude == nullptr)
  53. {
  54. rv = -1;
  55. printf("func getKeybyValue() err:%d pKeyValude == nullptr", rv);
  56. return rv;
  57. }
  58. if (pKey == nullptr)
  59. {
  60. rv = -1;
  61. printf("func getKeybyValue() err:%d pKey == nullptr ", rv);
  62. return rv;
  63. }
  64. if (pValude == nullptr)
  65. {
  66. rv = -1;
  67. printf("func getKeybyValue() err:%d pValude == nullptr", rv);
  68. return rv;
  69. }
  70. // 1、在pKeyValude中查找是否有关键字pKey
  71. p = strstr(pKeyValude, pKey);
  72. if (p == nullptr)
  73. {
  74. rv = -1;
  75. printf("func getKeybyValue() err:%d 查找关键字pKey出错", rv);
  76. return rv;
  77. }
  78. p = p + strlen(pKey); // 为下一次检索做准备
  79. // 2、有没有=
  80. p = strstr(p, "=");
  81. if (p == nullptr)
  82. {
  83. // 大字符串中没有=
  84. rv = -1;
  85. printf("func getKeybyValue() err:%d 查找关键字=出错", rv);
  86. return rv;
  87. }
  88. // 为下一次提取valude做准备
  89. p = p + 1; // p = p+strlen('=');
  90. // 3、提取按照要求的valude
  91. //rv = strRemoveSpace(buf, p);
  92. rv = strRemoveSpace(pValude, p);
  93. if (rv != 0)
  94. {
  95. printf("func strRemoveSpace() err:%d \n", rv);
  96. return rv;
  97. }
  98. return 0;
  99. }
  100. int main()
  101. {
  102. int ret = 0;
  103. //char pKeyValude[] = "key1=valude1";
  104. //char pKeyValude[] = "key1=valude1 ";
  105. char pKeyValude[] = "key1= valude1 ";
  106. char pKey[] = "key1";
  107. char pValude[1024] = { 0 };
  108. ret = getKeybyValue(pKeyValude, pKey, pValude);
  109. if (ret != 0)
  110. {
  111. printf("func getKeybyValue() err:%d \n", ret);
  112. return ret;
  113. }
  114. printf("Valude:%s \n", pValude);
  115. //system("pause");
  116. return 0;
  117. }




posted @ 2016-06-19 17:22  -刀狂剑痴-  阅读(332)  评论(0编辑  收藏  举报