For my master Thesis I need to write a program in C++ but I am not at all a programmer. It is also my first program in C++, I used to program for school in Java. My problem is the following. I have these textfiles containing some data, looking like this: In case you can't see the picture:
index date month WaveState WindState
0 2015-01-01 00:00:00 1 9.0 8.0
1 2015-01-01 04:00:00 1 9.0 7.0
2 2015-01-01 08:00:00 1 9.0 8.0
3 2015-01-01 12:00:00 1 9.0 9.0
4 2015-01-01 16:00:00 1 9.0 8.0
5 2015-01-01 20:00:00 1 9.0 7.0
6 2015-01-02 00:00:00 1 9.0 4.0
7 2015-01-02 04:00:00 1 9.0 2.0
8 2015-01-02 08:00:00 1 9.0 1.0
9 2015-01-02 12:00:00 1 9.0 3.0
10 2015-01-02 16:00:00 1 9.0 4.0
and so on.
Now i need to extract from these textfiles only the numbers considering 'windstate' and 'wavestate'. I would like to write these to a vector so I can easily use them further in my program.
This is the code I wrote so far:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
vector <string> dataVector;
int main()
{
string line;
ifstream myfile("wm994.txt");
if(myfile.is_open())
{
while (getline(myfile,line))
{
dataVector.push_back(line);
}
myfile.close();
}
else cout << "Woops, couldn't open file!" << endl;
for (unsigned i = 0; i<dataVector.size(); i++)
{
cout << dataVector.at(i) << endl;
}
return 0;
}
But of course my result looks like this. In case you can't see the picture I will describe it for you. I think that in every location of the vector an entire row of the textfile is saved as a String. But how can I access the 2 separate parts 'winddata' and 'wavedata' then? I hope to write something that puts every seperate part of the textfile on a seperate location in the vector so i know what locations to access then to get my winddata or wavedata numbers. But I really don't know how to do this.. I tried something like this:
while (myfile)
{
// read stuff from the file into a string and print it
myfile>> dataVector;
}
But this of course isn't working. Can I do something like this? That only the white space between my seperate pieces of text is skipped and these pieces are located in a new location in the vector?
I really hope someone can help me with this problem. I feel completely lost. Thank you in advance.