olexander_dan

Untitled

Apr 25th, 2019
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.61 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <time.h>
  5.  
  6. std::string getFilePath();
  7. size_t findBiggestNum(std::ifstream&);
  8.  
  9. int main() {
  10.     double timeS = clock();
  11.    
  12.     std::ifstream inputFileS;
  13.     while (true) {
  14.         std::string inputFilePath = getFilePath();
  15.         inputFileS.open(inputFilePath, std::ios_base::in);
  16.         if (inputFileS.fail())
  17.             inputFilePath.clear();
  18.         else
  19.             break;
  20.     }
  21.  
  22.     std::cout << "The biggest num in file is: " << findBiggestNum(inputFileS) << std::endl;
  23.  
  24.     double timeE = clock();
  25.     std::cout << "Time: " << (timeE - timeS) / (1000 * CLOCKS_PER_SEC) << std::endl;
  26.     system("pause");
  27.     return 0;
  28. }
  29.  
  30. std::string getFilePath() {
  31.     std::string filePath = "";
  32.     std::cout << "\nEnter the path to the input file or its name: ";
  33.     std::cin.clear();
  34.     getline(std::cin, filePath);
  35.     //if file path is empty -> save the output to the out.txt in the root folder
  36.     std::string rootFolder = "D:/Studying/Programming/LABS/Labka 2.5 v.1/Labka 2.5 v.1/";
  37.     if (filePath.empty())
  38.         filePath = rootFolder + "out.txt";
  39.     //if user didn't provided full adress -> create a new file in the root folder
  40.     else if (filePath.find('\\') == std::string::npos and filePath.find('/') == std::string::npos)
  41.         filePath.insert(0, rootFolder); // prepend address of the root folder
  42.     filePath.append(".txt"); // append extension of a text file
  43.     return filePath;
  44. }
  45.  
  46. size_t findBiggestNum(std::ifstream& ifs) {
  47.     size_t biggestNum = 0;
  48.     size_t checkVal = 0;
  49.     while (ifs.peek() != std::ifstream::traits_type::eof()) {
  50.         ifs >> checkVal;
  51.         if (checkVal > biggestNum)
  52.             biggestNum = checkVal;
  53.     }
  54.     return biggestNum;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment