-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadArrayFile.cpp
112 lines (87 loc) · 1.9 KB
/
readArrayFile.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
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include "readArrayFile.h"
int readArrayFile::readIntArrayFile(std::string filename)
{
std::ifstream fin;
fin.open(filename);
if (!fin.is_open())
{
std::cout << "Cannot open specified file." << '\n';
return -1;
}
else
{
std::cout << filename << " is opened succesfully." << '\n';
fin >> this->int_array_size;
this->intArray = new int[this->int_array_size];
for (int i = 0; i < this->int_array_size; i++)
{
if (fin.eof())
{
return -2;
}
fin >> this->intArray[i];
}
return 0;
}
}
int readArrayFile::readFloatArrayFile(std::string filename)
{
std::ifstream fin;
fin.open(filename);
if (!fin.is_open())
{
std::cout << "Cannot open specified file." << '\n';
return -1;
}
else
{
std::cout << filename << " is opened succesfully." << '\n';
fin >> this->float_array_size;
this->floatArray = new float[this->float_array_size];
for (int i = 0; i < this->float_array_size; i++)
{
if (fin.eof())
{
std::cout << "Test case" << '\n';
return -2;
}
fin >> this->floatArray[i];
}
return 0;
}
}
int readArrayFile::getIntArraySize()
{
return this->int_array_size;
}
int *readArrayFile::getIntArrayPointer()
{
if (this->int_array_size <= 0 || !this->intArray)
{
return NULL;
}
int *intArrayCopy = new int[this->int_array_size];
std::copy(this->intArray, this->intArray + this->int_array_size, intArrayCopy);
return intArrayCopy;
}
int readArrayFile::getFloatArraySize()
{
return this->float_array_size;
}
float *readArrayFile::getFloatArrayPointer()
{
if (this->float_array_size <= 0 || !this->floatArray)
{
return NULL;
}
float *floatArrayCopy = new float[this->float_array_size];
std::copy(this->floatArray, this->floatArray + this->float_array_size, floatArrayCopy);
return floatArrayCopy;
}
readArrayFile::~readArrayFile()
{
if (this->intArray)
delete[] this->intArray;
if (this->floatArray)
delete[] this->floatArray;
}