-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.h
More file actions
71 lines (66 loc) · 1.95 KB
/
util.h
File metadata and controls
71 lines (66 loc) · 1.95 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
70
71
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using std::vector;
using std::string;
string ReadFile(string file_name) {
// Create a string to hold the data
string data;
// Create a string to hold each line of the file
string line;
// Create an input file stream
std::ifstream file;
// Open the file
file.open(file_name);
// Check if the file is open
if (file.is_open()) {
// While there is still data to read
while (getline(file, line)) {
// Add the line to the data string
data += line;
// Add a newline character
data += "\n";
}
// Close the file
file.close();
}
// Return the data
return data;
}
vector<vector<float>> ReadCsvMatrix(string csv_string) {
// Create a vector to hold the matrix
vector<vector<float>> matrix;
// Create a vector to hold the current row
vector<float> row;
// Create a string to hold the current value
string value;
// Loop through the characters in the string
for (int i = 0; i < csv_string.size(); i++) {
// If the current character is a comma
if (csv_string[i] == ',') {
// Add the current value to the row
row.push_back(std::stof(value));
// Clear the value
value = "";
}
// If the current character is a newline
else if (csv_string[i] == '\n') {
// Add the current value to the row
row.push_back(std::stoi(value));
// Clear the value
value = "";
// Add the current row to the matrix
matrix.push_back(row);
// Clear the row
row.clear();
}
// If the current character is not a comma or newline
else {
// Add the current character to the value
value += csv_string[i];
}
}
// Return the matrix
return matrix;
}