So I am working on a home automation project so I can control my airconditioner though an arduino, but to store the data from the various commands I need to convert them into binary I am trying to write a program to do that for me but I have run into a problem, there is no output at all. Of cause it is still unfinished as I need to later convert each of the numbers from the text file into binary but I am stuck here for the moment.
Raw is an output file (ofstream). But it is opened with the mode ios::in which suggests you want to read input from it. But no, at line 16,
Raw << file;
you are outputting the empty string, misleadingly named "file", and then the file is closed. As a result, nothing is written to the file, and nothing is read from it.
I'll assume from the title of this post, and from the rest of the code that what you really want to do is to open an input file and read at least one line of text from it.
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
usingnamespace std;
int main()
{
ifstream Raw("Raw.txt"); // open file for input
if (!Raw)
{
cout << "Raw file is not open\n";
return 1;
}
string line;
getline(Raw, line); // read first line from file
Raw.close();
vector<string> CSV;
stringstream ss (line); // use stringstream to parse line
string word;
while (getline(ss, word, ',')) // read line from ss delimited by comma
{
CSV.push_back(word);
}
for (size_t v = 0; v < CSV.size(); v++)
{
cout << CSV.at(v) << '\n';
}
}
Which is to be expected, that is what is contained in the text document, as the aircon reads -1000+ as a 1 and -300 to -400 as a 0 I have tried to use an if else statement to convert them over.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
for (size_t v = 0; v < CSV.size(); v++)
{
//cout << CSV.at(v);
if (v > 1) {
cout << ' ';
}
elseif (v >= -300 || v <= -400)
{
cout << 1;
}
else (v >= -1000 || v <= -1400);
{
cout << 0;
}
}
I have tried all possibilities I can think of in term of the < > and position of the numbers but it will normally give out all 0s or all 1s. And here was me thinking this would be the easy part haha.