Need help pulling information from an outside file

I'm working on a program from class where we need to pull specific data from an outside file.

This is the file I am puling from. It' called Condensed_Data.dat

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<VoltAmpereReactiveTimeSeries dataItemId="electric_61" timestamp="2014-07-08T18:14:17.716Z" name="kvar_a" sequence="121794208" sampleCount="25">
3937 3938 3940 3944 3943 3943 3939 3937 3937 3931 3925 3921 3919 3920 3918 3918 3920 3922 3919 3915 3913 3910 3907 3904 3899

<VoltAmpereReactiveTimeSeries dataItemId="electric_62" timestamp="2014-07-08T18:14:18.716Z" name="kvar_b" sequence="121794204" sampleCount="25">
5805 5805 5807 5807 5806 5803 5801 5797 5794 5790 5786 5784 5781 5780 5777 5776 5779 5779 5784 5789 5794 5800 5803 5807 5808

<VoltAmpereReactiveTimeSeries dataItemId="electric_63" timestamp="2014-07-08T18:14:19.716Z" name="kvar_c" sequence="121794206" sampleCount="25">
7128 7134 7136 7136 7143 7141 7141 7141 7140 7143 7142 7144 7143 7147 7154 7155 7158 7159 7161 7162 7160 7163 7163 7166 7170

<WattageTimeSeries dataItemId="electric_64" timestamp="2014-07-08T18:14:20.716Z" name="kw_a" sequence="121794207" sampleCount="25">
19437 19430 19416 19414 19423 19429 19426 19420 19421 19435 19440 19425 19414 19409 19413 19420 19412 19407 19414 19423 19439 19440 19434 19441 19455

<WattageTimeSeries dataItemId="electric_65" timestamp="2014-07-08T18:14:21.716Z" name="kw_b" sequence="121794209" sampleCount="25">
8795 8792 8794 8801 8804 8800 8802 8805 8810 8810 8801 8793 8795 8798 8796 8790 8784 8789 8797 8798 8796 8797 8805 8815 8818

<WattageTimeSeries dataItemId="electric_66" timestamp="2014-07-08T18:14:22.716Z" name="kw_c" sequence="121794205" sampleCount="25">
12572 12579 12585 12584 12581 12591 12602 12602 12598 12596 12593 12601 12596 12588 12596 12599 12609 12616 12612 12618 12629 12636 12637 12629 12623

The information I need from the file is the dataItemID, timestamp, name, and the numbers on the second line to average them out later.

If anyone is able to help e with this that would be great!
Last edited on

it is a binary file
why don't you use reading from binary file functionality?

By the way in what data structure are you reading in?


That is what i'm trying to do. I'm just having trouble separating the information that I need from what I don't and putting it into an another ouput file. I'm reading the data into strings.
Last edited on
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
int secondReading()
{
	ifstream In_File;
    ofstream Out_File;
    string data_text;                       //variable defined for each line of text from .xml file
    string place_indicator;                //variable used to locate desired line of text from .xml file
    string place_indicator2;        //variable used to determine if data is found from desired line of text (data values)

	In_File.open("Condensed_Data.dat");
    Out_File.open("Condensed_Data2.dat");           //any file format is appropriate for Out_File to be outputted as

	getline(In_File, data_text);        //pulls first line of text from .xml file

	while(In_File)
		place_indicator = data_text.substr(0,data_text.find(' '));        //variable defined by first word of each line of text
                if(place_indicator == "dataItemId=")

                {
                        Out_File << data_text << endl;
                        place_indicator2 = data_text.find(">UNAVAILABLE<");
                        if(place_indicator2 != ">UNAVAILABLE<")

                        {
                                getline(In_File, data_text);
                                Out_File << data_text << endl;
                                Out_File << endl;
                        }
                        else
                        {
                                getline(In_File, data_text);
                        }
                }
                else if(place_indicator == "timestamp=")
                {
                        Out_File << data_text << endl;
                        place_indicator2 = data_text.find(">UNAVAILABLE<");
                        if(place_indicator2 != ">UNAVAILABLE<")
                        {
                                getline(In_File, data_text);
                                Out_File << data_text << endl;
                                Out_File << endl;
                        }
                        else
                        {
                                getline(In_File, data_text);
                        }
				}
				else if(place_indicator == "name=")
                {
                        Out_File << data_text << endl;
                        place_indicator2 = data_text.find(">UNAVAILABLE<");
                        if(place_indicator2 != ">UNAVAILABLE<")
                        {
                                getline(In_File, data_text);
                                Out_File << data_text << endl;
                                Out_File << endl;
                        }
                        else
                        {
                                getline(In_File, data_text);
                        }
                }
                else                                                                                                                //if the line does not start with any of the previous...
                {
                        getline(In_File, data_text);                                                        //pull next line of text and restart while loop

        }

		In_File.close();        //file will close; .xml file remains unchanged
        Out_File.close();	//file will close; new file is created in project folder; file is now written with what the function has outputted to Out_File
        
		system("pause");

        return 0;

}


This is what I have for the code. The program runs without errors but it can't find the specific strings that I need. Because of that nothing is put into the new output file. Any Suggestions?
Last edited on
You wrote if(place_indicator == "dataItemId=")

try with compare function of string class same goes to not equal

http://www.cplusplus.com/reference/string/string/compare/
Last edited on
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
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{
	ifstream In_File;
	ofstream Out_File;
	string data_text;
	string place_indicator;
	std::string str1 ("dataItemId");
	std::string str2 ("timestamp");
	std::string str3 ("name");

	In_File.open("Condensed_Data.dat");
	Out_File.open("Condensed_Data2.dat");

	getline(In_File, data_text);

	do
	{
		place_indicator = data_text.substr(0,data_text.find(' '));
		if (str1.compare(place_indicator) == 0)
		{
			Out_File << data_text << endl;
		}
		else if (str2.compare(place_indicator) == 0)
		{
			Out_File << data_text << endl;
		}
		else if (str3.compare(place_indicator) == 0)
		{
			Out_File << data_text << endl;
		}
		else
			getline(In_File, data_text);

	} while (In_File);

	In_File.close();
	Out_File.close();

	system("pause");

	return 0;
}


This what I have now but it's still not putting the information into the out file.
bump
Topic archived. No new replies allowed.