-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcsv2code.cpp
126 lines (107 loc) · 3.53 KB
/
csv2code.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
using namespace std;
//zypherr aka yashraj
// Function to split a string based on a delimiter
vector<string> splitString(const string &input, char delimiter) {
vector<string> result;
stringstream ss(input);
string token;
while (getline(ss, token, delimiter)) {
result.push_back(token);
}
return result;
}
int main() {
// Input CSV file name
string csvFileName = "C:\\Users\\yashr\\Desktop\\csv2code\\data_captain.csv";
// Output text file name
string outputFileName = "captain.txt";
// Open the CSV file for reading
ifstream inputFile(csvFileName);
if (!inputFile.is_open()) {
cerr << "Error: Unable to open CSV file." << endl;
return 1;
}
// Open the output text file for writing
ofstream outputFile(outputFileName);
if (!outputFile.is_open()) {
cerr << "Error: Unable to open output text file." << endl;
inputFile.close();
return 1;
}
// Read and process each row of the CSV file
string rowData;
string ele = "";
vector<string> red, green, blue;
vector<string> rowValue;
bool f=1;
while (getline(inputFile, rowData)) {
// Split the row into individual column values
rowValue = splitString(rowData, ',');
if(f){
// rowValue.erase(rowValue.begin());
printf("%d\n", rowValue.size());
// for (int i = 0; i < rowValue.size(); i++)
// {
// cout<<rowValue[i]<<" ";
// }
f = !f;
}
int z=0;
for(int k=0; k<14; k++){
//below writes for 8 leds at a time
for(int i=0; i<3; i++){
int cnt=0;
while(cnt<8){
int l = (8*z)+cnt;
ele = ele.append(string(1, rowValue[l][i]));
cnt++;
}
if(i==0){
red.push_back(ele);
ele = "";
}
else if(i==1){
green.push_back(ele);
ele = "";
}
else{
blue.push_back(ele);
ele = "";
}
}
z++;
}
// Process the column values as needed
int k = 0;
for (const string &valuer : red) {
// Do something with each value (e.g., write to the output file)
outputFile << "arr_red["<< k++ << "]= B" << valuer << ";" << "\n";
}
outputFile << endl;
k=0;
for (const string &valueg : green) {
// Do something with each value (e.g., write to the output file)
outputFile << "arr_green["<< k++ << "]= B" << valueg << ";" << "\n";
}
outputFile << endl;
k=0;
for (const string &valueb : blue) {
// Do something with each value (e.g., write to the output file)
outputFile << "arr_blue["<< k++ << "]= B" << valueb << ";" << "\n";
}
outputFile << "rgb_clm();\ndelay(dl);\n" << endl;
red.clear();
green.clear();
blue.clear();
}
// Close the input and output files
inputFile.close();
outputFile.close();
printf("Data has been successfully written to ");
std::cout << outputFileName;
return 0;
}