PPL Assignment
IRM2015006
csv_creator.cpp
Go to the documentation of this file.
1 #ifndef CSV_CREATOR_H
2 #include "csv_creator.h"
3 #endif
4 
5 #include <stdexcept>
6 
7 CSVCreator::CSVCreator(std::string filename,
8  std::string type,
9  std::vector<std::string> headers,
10  std::vector<int> min_value,
11  std::vector<int> max_value)
12 {
13  if (headers.size()-1 != min_value.size() ||
14  headers.size()-1 != max_value.size())
15  throw std::invalid_argument ("Parameters of unequal length");
16 
17  this->fields = headers.size();
18  this->filename = filename;
19  this->type = type;
20  this->headers = headers;
21  this->min_value = min_value;
22  this->max_value = max_value;
23 }
24 
26 {
27  std::fstream file(this->filename.c_str(), std::ios::out | std::ios::trunc);
28 
29  /* some variables which will be required later */
30  std::vector <std::string>::iterator str_it;
31  std::vector <int>::iterator int_it;
32  int i, j;
33 
34  /* write all the headers */
35  for (i = 0; i < this->fields; i++)
36  file << this->headers[i] << ',';
37  file << std::endl;
38 
39  /* write data */
40  for (i = 0; i < n; i++) {
41  file << this->type << '-' << i << ',';
42  for (j = 0; j < this->fields-1; j++)
43  file << get_random_int(this->min_value[j], this->max_value[j]) << ',';
44  file << std::endl;
45  }
46  file.close();
47 }
48 
49 int CSVCreator::get_random_int(int min, int max)
50 {
51  return min + (rand() % (max-min+1));
52 }
std::string filename
Definition: csv_creator.h:16
std::vector< int > max_value
Definition: csv_creator.h:15
int get_random_int(int min, int max)
Definition: csv_creator.cpp:49
void generate(int n)
Definition: csv_creator.cpp:25
std::string type
Definition: csv_creator.h:17
std::vector< int > min_value
Definition: csv_creator.h:14
std::vector< std::string > headers
Definition: csv_creator.h:13
CSVCreator(std::string filename, std::string type, std::vector< std::string > headers, std::vector< int > min_value, std::vector< int > max_value)
Definition: csv_creator.cpp:7