-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.cpp
51 lines (41 loc) · 1.16 KB
/
generate.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
#include <cstring>
void printUsage(char* name) {
std::cout << "usage: " << name << " -n number_to_generate -o output_file" << std::endl;
}
void printInvalidArgument() {
std::cout << "invalid argument" << std::endl;
}
int main(int argc, char *argv[]) {
/* Must provide -n and -o argument */
if (argc != 5 || strncmp(argv[1], "-n", 3) != 0 || strncmp(argv[3], "-o", 3) != 0) {
printUsage(argv[0]);
return 1;
}
/* Parse n */
int n = 10;
try {
int nArg = std::stoi(argv[2]);
n = nArg;
} catch (std::invalid_argument &e) {
printInvalidArgument();
printUsage(argv[0]);
return 1;
}
/* Open output file */
char* outputFilename = argv[4];
std::ofstream out;
out.open(outputFilename, std::ofstream::out);
/* Seed random number generator with current time */
std::srand(std::time(0));
/* Print n random numbers */
for (int i = 0; i < n; i++) {
out << std::rand() << "\n";
}
/* Flush and close output file */
out.close();
return 0;
}