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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
// Name: msglm
// Date:
// Program Name:
// Description:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
// Named constants
int ARR_SIZE = 50; //Personally, i'd pass this as an env var or a flag
int main() {
//Variable declaration
string tmpArr[ARR_SIZE]; //Probably could pass through the file first to get the size first and declare this, but that's past this program's scope.
int epoch = 0; //both counts up and is the length of the array.
//Vars necessary to invert an array
int start = 0;
string tmp;
int end = 0;
//It's a shame this isn't STDIN and STDOUT
ifstream inFile;
ofstream outFile;
// User input
if (inFile) {
inFile.open("in.txt");
outFile.open("out.txt");
//Read the line, put it at the epoch in the array, also check if EOF. if EOF, abort.
while(getline(inFile, tmpArr[epoch]) && inFile.peek() != EOF) {
cout << tmpArr[epoch] << endl;
outFile << tmpArr[epoch] << endl;
epoch = epoch + 1;
}
//Epoch is assumed to be the length of the variable at this point, so will use accordingly
end = epoch;
//exchange the positions of the first and last var in an array while slowly moving to the center
//i.e
//on first pass of the while look will turn [1, 2, 3, 4] into [4, 2, 3, 1]
//and the second will turn [4, 3, 2, 1]
//
//I opted to use this algorithm to not create any more data
while (start < end) {
tmp = tmpArr[start];
tmpArr[start] = tmpArr[end];
tmpArr[end] = tmp;
start = start + 1;
end = end - 1;
}
//finally, output the reversed array and dump it into a file
for(int i=0;i<epoch+1;i++) {
cout << tmpArr[i] << endl;
outFile << tmpArr[i] << endl;
}
return 0;
} else {
cout << "ERROR. Input File doesn't exist!";
return 1;
}
}
/*
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License Version 3 ONLY as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
|