-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtractValuesFile.cpp
More file actions
69 lines (64 loc) · 1.71 KB
/
ExtractValuesFile.cpp
File metadata and controls
69 lines (64 loc) · 1.71 KB
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
// student no: A00030840
#include "ExtractValuesFile.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
int ExtractValuesFile::number_elements(string fileName) {
string line;
ifstream myfile(fileName);
int lineCount = 0;
if (myfile.is_open())
{
while (getline(myfile, line))
{
if (lineCount == 0) {
return std::stoi(line);
}
}
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
int * ExtractValuesFile::strip_elements(string rawDataElements, int numberRawElements) {
std::stringstream ss(rawDataElements);
int* dataElements = new int[numberRawElements];
int lineCount = 0;
for (int i; ss >> i;) {
dataElements[lineCount] = i;
if (ss.peek() == ' ') {
ss.ignore();
}
lineCount++;
}
if (lineCount != numberRawElements) {
alert.errorMessage("more elements were declared, than elements in the array. Please fix this up, before continuing.");
delete[] dataElements;
return {};
}
else {
return dataElements;
}
delete[] dataElements;
return {};
}
int * ExtractValuesFile::get_elements(string fileName, int numberRawElements) {
string line;
ifstream myfile(fileName);
int lineCount = 0;
if (myfile.is_open())
{
while (getline(myfile, line))
{
if (lineCount == 1) {
return strip_elements(line, numberRawElements);
}
lineCount++;
}
myfile.close();
}
else cout << "Unable to open file";
return {};
}