1

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.

Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
  • Can you [edit] your question to include the pasted results of your program? There is no *picture* to be seen, and text would be easier to analyze than a picture. – Drew Dormann Mar 12 '15 at 18:31
  • Welcome. Please provide a link to your picture (someone with more privileges might even edit your question and replace the link with the actual picture). – mins Mar 12 '15 at 18:37

2 Answers2

0

You should split each line you get from the file and access the 3rd (WaveState) and 4th (WindState) element of the vector that you would get back. Here is an example on how to split a string.

String Split

Community
  • 1
  • 1
Serge Nassar
  • 166
  • 5
  • Thank you so much for helping me. I used this: #include #include #include using namespace std; int main() { string str("Split me by whitespaces"); string buf; // Have a buffer string stringstream ss(str); // Insert the string into a stream vector tokens; // Create vector to hold our words while (ss >> buf) tokens.push_back(buf); } and it works now perfectly. – Thesis_Student_BE Mar 14 '15 at 14:33
0

If you compile support C++11 offer opportunities to use regex library:

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <regex>

int main()
{
    std::vector <std::pair<double,double>> dataVector;
    std::string line;
    std::ifstream myfile("1.txt");
    std::smatch m;
    std::regex e (".*(\\w+\\.\\w+) (\\w+\\.\\w+)$");

    if(myfile.is_open())
    {
        while (getline(myfile,line))
        {
            std::regex_search (line,m,e);
            dataVector.push_back(std::pair<double,double>(std::stod(m[1].str()),std::stod(m[2].str())));
        }
        myfile.close();
    }
    else 
    {
    std::cout << "Woops, couldn't open file!" << std::endl;
    return -1;
    }

    for (auto i:dataVector)
        std::cout<<i.first<<" "<<i.second<<std::endl;

    return 0;
}