Advertisement
Guest User

Untitled

a guest
Jul 7th, 2015
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1. #include <io.h>
  2. #include <string>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. /*
  8. * 功能:遍历文件和子文件夹内的文件
  9. * 参数:
  10. * 状态:
  11. */
  12. void getFiles(string path, vector<string>& files)
  13. {
  14. //文件句柄
  15. long hFile = 0;
  16. //文件信息
  17. struct _finddata_t fileinfo;
  18. string p;
  19. if ((hFile = _findfirst(p.assign(path).append("\\*.JPG").c_str(), &fileinfo)) != -1)
  20. {
  21. do
  22. {
  23. //如果是目录,迭代之,如果不是,加入列表
  24. if ((fileinfo.attrib &_A_SUBDIR))
  25. {
  26. if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
  27. getFiles(p.assign(path).append("\\").append(fileinfo.name), files);
  28. }
  29. else
  30. {
  31. files.push_back(p.assign(path).append("\\").append(fileinfo.name));
  32. }
  33. } while (_findnext(hFile, &fileinfo) == 0);
  34.  
  35. _findclose(hFile);
  36. }
  37. }
  38.  
  39.  
  40. /*
  41. * 功能:输入文件的全路径获得文件名不带扩展
  42. * 参数:
  43. * 状态:
  44. */
  45. string getFilename(string s)
  46. {
  47. char sep = '/';
  48. char sepExt = '.';
  49.  
  50. #ifdef _WIN32
  51. sep = '\\';
  52. #endif
  53.  
  54. size_t i = s.rfind(sep, s.length());
  55. if (i != string::npos) {
  56. string fn = (s.substr(i + 1, s.length() - i));
  57. size_t j = fn.rfind(sepExt, fn.length());
  58. if (i != string::npos) {
  59. return fn.substr(0, j);
  60. }
  61. else{
  62. return fn;
  63. }
  64. }
  65. else{
  66. return ""; //若没有找到则返回空值
  67. }
  68. }
  69.  
  70. /*
  71. * 功能:输入文件的全路径获得带扩展的文件名
  72. * 参数:
  73. * 状态:
  74. */
  75. string getFilenameWithExt(string s)
  76. {
  77. char sep = '\\';
  78.  
  79. size_t i = s.rfind(sep, s.length()); //找到字符‘\\’最后一次出现的位置
  80. if (i != string::npos){
  81. string fn = (s.substr(i + 1, s.length() - i)); //取得带扩展的文件名
  82. return fn;
  83. }
  84. }
  85.  
  86.  
  87. /*
  88. * 功能:输入文件的全路径获得文件名不带扩展
  89. * 参数:
  90. * 状态:
  91. */
  92. string getFilenameWithNoExt(string s)
  93. {
  94. char sep = '/';
  95. char sepExt = '.';
  96.  
  97. #ifdef _WIN32
  98. sep = '\\';
  99. #endif
  100.  
  101. size_t i = s.rfind(sep, s.length());
  102. if (i != string::npos) {
  103. string fn = (s.substr(i + 1, s.length() - i));
  104. size_t j = fn.rfind(sepExt, fn.length());
  105. if (i != string::npos) {
  106. return fn.substr(0, j);
  107. }
  108. else{
  109. return fn;
  110. }
  111. }
  112. else{
  113. return ""; //若没有找到则返回空值
  114. }
  115. }
  116.  
  117.  
  118. string getFilenameWithNoExt_m(string s)
  119. {
  120. char sep = '\\';
  121. char sepExt = '.';
  122.  
  123.  
  124. size_t i = s.rfind(sep, s.length()); //找到字符‘/’最后一次出现的位置
  125. if (i != string::npos)
  126. {
  127. string fn = (s.substr(i + 1, s.length() - i)); //取得带扩展的文件名
  128. size_t j = fn.rfind(sepExt, fn.length()); //找到‘.’最后一次出现的位置
  129. if (j != string::npos) {
  130. return fn.substr(0, j); //返回
  131. }
  132. else{
  133. return fn;
  134. }
  135. }
  136. else{
  137. size_t k = s.rfind(sepExt, s.length());
  138. return s.substr(0, k);
  139. }
  140. }
  141.  
  142.  
  143.  
  144. /*
  145. * 功能:输入文件的全路径获得文件名不带扩展
  146. * 参数:
  147. * 状态:
  148. */
  149. static string getFilename(string s)
  150. {
  151. char sep = '/';
  152. char sepExt = '.';
  153.  
  154. #ifdef _WIN32
  155. sep = '\\';
  156. #endif
  157.  
  158. size_t i = s.rfind(sep, s.length());
  159. if (i != string::npos) {
  160. string fn = (s.substr(i + 1, s.length() - i));
  161. size_t j = fn.rfind(sepExt, fn.length());
  162. if (i != string::npos) {
  163. return fn.substr(0, j);
  164. }
  165. else{
  166. return fn;
  167. }
  168. }
  169. else{
  170. return ""; //若没有找到则返回空值
  171. }
  172. }
  173.  
  174. /*
  175. * 功能:输入文件的全路径获得除扩展外的包含路径和文件名的字符串
  176. * 参数:
  177. * 状态:
  178. */
  179. static string getFilePath(std::string s)
  180. {
  181. //std::string s = replace_all(s1, "\\", "\\\\");
  182. char sepExt = '.';
  183. size_t i = s.rfind(sepExt, s.length());
  184. if (i != std::string::npos)
  185. {
  186. std::string path = s.substr(0, i);
  187. return path;
  188. }
  189. else
  190. {
  191. std::cout << s << "is not a file name" << std::endl;
  192. return "";
  193. }
  194.  
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement