Advertisement
Guest User

Untitled

a guest
Jan 20th, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. #include <io.h>
  2. #include <windows.h>
  3. #include <iostream>
  4. #include <fstream>
  5. #include <string>
  6. #include <vector>
  7.  
  8. using namespace std;
  9.  
  10. /////////////////////////////////////////////////////////////////////////////////////////////////////
  11. // FUNKCJA PRZYJMUJACA SCIEZKE DO FOLDERU I ZWRACAJACA TABLICE (VECOTR) Z NAZWAMI PODFOLDEROW //
  12. /////////////////////////////////////////////////////////////////////////////////////////////////////
  13. vector <string> findDir(string path)
  14. {
  15. path += "\\*";
  16. vector <string> subdir;
  17. _finddata_t fileData;
  18. const char *address = path.c_str();
  19. long handler = _findfirst(address, &fileData);
  20. if (handler != (-1))
  21. {
  22. string fileName;
  23. int findNext;
  24.  
  25. do
  26. {
  27. fileName = fileData.name;
  28. if (fileData.attrib == _A_SUBDIR && fileName != "." && fileName != "..")
  29. {
  30. subdir.push_back(fileName);
  31. }
  32. findNext = _findnext(handler, &fileData);
  33. }
  34. while (!findNext);
  35. _findclose(handler);
  36. _findclose(findNext);
  37. }
  38. else
  39. _findclose(handler);
  40. return subdir;
  41. }
  42.  
  43. /////////////////////////////////////////////////////////////////////////////////////////////////////
  44. // FUNKCJA PRZYJMUJACA SCIEZKE DO FOLDERU ORAZ ROZSZERZENIE PLIKOW I ZWRACAJACA TABLICE (VECOTR) //
  45. // Z NAZWAMI PLIKOW O ZADANYM ROZSZERZENIU //
  46. /////////////////////////////////////////////////////////////////////////////////////////////////////
  47. vector <string> findFiles(string path, string extension)
  48. {
  49. path += "\\*" + extension;
  50. vector <string> ourFiles;
  51. _finddata_t fileData;
  52. const char *address = path.c_str();
  53. long handler = _findfirst(address, &fileData);
  54.  
  55. if (handler != (-1))
  56. {
  57. string fileName; // = fileData.name;
  58. //ourFiles.push_back(fileName);
  59. int findNext; // = _findnext(handler, &fileData);
  60.  
  61. do
  62. {
  63. fileName = fileData.name;
  64. ourFiles.push_back(fileName);
  65. findNext = _findnext(handler, &fileData);
  66. }
  67. while (!findNext);
  68. _findclose(handler);
  69. _findclose(findNext);
  70. }
  71. else
  72. {
  73. cout << "Brak plikow o zadanym rozszerzeniu" << endl;
  74. _findclose(handler);
  75. }
  76. return ourFiles;
  77. }
  78.  
  79. /////////////////////////////////////////////////////////////////////////////////////////////////////
  80. // FUNKCJA PRZYJMUJACA SCIEZKE DO PLIKU I ZWRACAJACA LICZBE LITER W PLIKU //
  81. /////////////////////////////////////////////////////////////////////////////////////////////////////
  82. int linesCount(string path)
  83. {
  84. int numOFletters = 0;
  85. string line;
  86. ifstream myFile(path);
  87. if (myFile.good())
  88. {
  89. while (getline(myFile, line))
  90. {
  91. //cout << line.size() << endl;
  92. for (int i = 0; i < line.size(); i++)
  93. {
  94. if ( (int(line.at(i)) >= 65 && int(line.at(i)) <= 90) || (int(line.at(i)) >= 97 && int(line.at(i)) <= 122))
  95. {
  96. ++numOFletters;
  97. }
  98. }
  99. }
  100. return numOFletters;
  101. }
  102. else
  103. return -1;
  104. }
  105.  
  106.  
  107.  
  108. int main()
  109. {
  110.  
  111. string plik = "D:\\Elektronika i Telekomunikacja\\V semestr\\JPO\\Projekt\\test.txt";
  112. cout << linesCount(plik) << endl;
  113.  
  114. string path = "D:\\Elektronika i Telekomunikacja\\V semestr\\JPO\\Projekt";
  115. string ext = ".txt";
  116. vector <string> sciezka;
  117. sciezka = findFiles(path, ext);
  118. for (int i = 0; i < findFiles(path, ext).size(); i++)
  119. {
  120. cout << findFiles(path, ext)[i] << endl;
  121. }
  122.  
  123. sciezka = findDir(path);
  124. for (int i = 0; i < findDir(path).size(); i++)
  125. {
  126. cout << findDir(path)[i] << endl;
  127. }
  128.  
  129. system("PAUSE");
  130.  
  131. return 0;
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement