Assignment-1/general_functions.cpp
2019-09-30 08:58:41 +00:00

66 lines
1.4 KiB
C++

// License: AGPLv3 or later. https://www.gnu.org/licenses/licenses.html
#include <fstream>
#include <iostream>
#include <random>
# include "general_functions.h"
using namespace std;
vector<string> get_textfile_lines(std::string filename)
{
ifstream file(filename);
vector<string> file_lines;
if (! file.good())
{
cerr << "Failed to open/read file: " << filename\
<< ". Non-exhaustive list of possible reasons:\n"\
<< "1. File does not exist.\n"\
<< "2. Unable to read file due to:\n"\
<< "2.1 Don't have read access to file\n"\
<< "2.2 File is opened elsewhere and some program have a "\
<< "lock on file.\n";
exit(2);
}
cout << "Reading file: " << filename << endl;
while (file.good())
{
string line;
getline(file, line);
file_lines.push_back(line);
}
file.close();
return file_lines;
}
int random_int(int lower_bound, int upper_bound)
{
static std::random_device seed;
static std::mt19937 random_number_generator(seed());
std::uniform_int_distribution<std::mt19937::result_type> range(
lower_bound, upper_bound);
return range(random_number_generator);
}
bool is_string_some_positive_number(std::string a_string)
{
for (int i=0; i < a_string.size(); i++)
{
string character(1, a_string[i]);
try
{
stoi(character);
}
catch(...)
{
return false;
}
}
return true;
}