Reading and outputting CSV from .txt

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.

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
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

string file;

int main()
{
	ofstream Raw;
	Raw.open("Raw.txt", ios::in);
	Raw << file;
	Raw.close();

	vector<string> CSV;

	stringstream ss (file);

	while (ss >> file)
	{
		CSV.push_back(file);

		if (ss.peek() == ',')
			ss.ignore();
		
	}
	for (int v = 0; v < CSV.size(); v++)
	{
		cout << CSV.at(v) << '\n';
	}
	cin.clear();
	cin.ignore(255, '\n');
	cin.get();

    return 0;
}
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.

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
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>

using namespace 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';
    }


}


Thanks that has got the correct numbers to output but I have run into another issue, getting them to convert to binary. The output from the code is

404 -332 464 -1176 408 -328 464 -388 404 -1180 404 -388 408 -1176 412 -324 468 -
384 408 -1176 412 -1176 408 -1180 408 -324 496 -1148 408 -384 408 -1180 408 -117
6 408 -1180 408 -384 408 -388 408 -328 460 -388 408 -1176 408 -1180 408 -384 408
 -1176 412 -1176 404 -388 408 -264 556 -1152 408 -332 460 -320 476 -1176 408 -38
8 404 -324 472 -1176 408 -1176 408 -384 440 -1148 408 -1180 408 -1176 408 -1176
412 -1176 412 -1176 408 -1176 408 -1176 408 -1180 408 -1176 408 -268 524 -336 46
0 -332 460 -328 464 -388 408 -380 412 -328 492 -264 504 -1176 408 -1176 440 -114
8 408 -1176 408 -384 412 -324 472 -320 468 -324 468 -384 432 -360 436 -360 408 -
384 436 -1152 408 -1176 408 -1180 432 -1152 412 -1172 412 -384 408 -1176 412 -38
0 412 -332 460 -384 412 -1172 412 -1176 408 -320 476 -1172 412 -328 468 -1172 43
6 -1152 436 -1144 440 -360 436 -252 540


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 << ' ';
		}
		else if (v >= -300 || v <= -400)
		{
			cout << 1;
		}
		else (v >= -1000 || v <= -1400);
		{
			cout << 0;
		}
	}


But the code I get back is.

1010 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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.
Topic archived. No new replies allowed.