To be honest, I do not fully understand the structure of your input file and what you want to achieve. People do really want to help you and therefore I recommend to add more data.
You should also compile your program, before posting. It will show you already there some syntax errors. Without compiling I can tell you that there is
- Missing semicolon after open function
- No string in double quotes in open function
- Capital Z in last statement
All these Bugs the compiler would show you. And, if you want to get help from SO members, you should at least present compiled code . . .
As said. I do not know exactly what you want to do. Anyway, please see below example:
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
int main()
{
// Open the input file
std::ifstream file("yourFileName.txt");
// We must check, if the file could be opened
if (file) {
std::string oneLine{};
// Read all lines from file
while(std::getline(file, oneLine)) {
// Copy line to a stringstream, so that we can extract the data fields
std::istringstream issLine(oneLine);
// Define Variables that will be read from the line
double x{0.0}, y{0.0}, z{0.0};
std::string date{};
// Extract the requested data
issLine >> date >> x >> y >> z;
//
// Do some work with that data
//
}
}
else {
std::cerr << "Could not open Input file\n";
}
return 0;
}
So first we define a variable file of type ifstream and handover the filename as a parameter for its constructor. This tries to open the file. The file will be closed automatically by the destructor, when the variable goes out of scope.
Then we check that if the file could be opened. This works, becuase the ! operator of the ifstream has been overloaded. If the file could not be opened, then we show an error message.
Next we use the getline function to read a complete line from the file. We will do that in a while loop so that we read one line after the other until we have read all lines in the file.
Now a little bit more tricky. Because we want to extract data, we copy the read line to an istringstream object. From such an object we can extract data.
We then extract all the data and you can do whatever you want with that.
Please look up all functions that I used in the internet and get and understanding. Read a C++Book.
Hope this helps a little.