Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <time.h>
- std::string getFilePath();
- size_t findBiggestNum(std::ifstream&);
- int main() {
- double timeS = clock();
- std::ifstream inputFileS;
- while (true) {
- std::string inputFilePath = getFilePath();
- inputFileS.open(inputFilePath, std::ios_base::in);
- if (inputFileS.fail())
- inputFilePath.clear();
- else
- break;
- }
- std::cout << "The biggest num in file is: " << findBiggestNum(inputFileS) << std::endl;
- double timeE = clock();
- std::cout << "Time: " << (timeE - timeS) / (1000 * CLOCKS_PER_SEC) << std::endl;
- system("pause");
- return 0;
- }
- std::string getFilePath() {
- std::string filePath = "";
- std::cout << "\nEnter the path to the input file or its name: ";
- std::cin.clear();
- getline(std::cin, filePath);
- //if file path is empty -> save the output to the out.txt in the root folder
- std::string rootFolder = "D:/Studying/Programming/LABS/Labka 2.5 v.1/Labka 2.5 v.1/";
- if (filePath.empty())
- filePath = rootFolder + "out.txt";
- //if user didn't provided full adress -> create a new file in the root folder
- else if (filePath.find('\\') == std::string::npos and filePath.find('/') == std::string::npos)
- filePath.insert(0, rootFolder); // prepend address of the root folder
- filePath.append(".txt"); // append extension of a text file
- return filePath;
- }
- size_t findBiggestNum(std::ifstream& ifs) {
- size_t biggestNum = 0;
- size_t checkVal = 0;
- while (ifs.peek() != std::ifstream::traits_type::eof()) {
- ifs >> checkVal;
- if (checkVal > biggestNum)
- biggestNum = checkVal;
- }
- return biggestNum;
- }
Advertisement
Add Comment
Please, Sign In to add comment