You have multiple problems, most of them regarding your reading and parsing:
Each record (each line) contains three values: A date-time stamp, and two numeric values. You read the date-time stamp (wrongly, I'll come back to that) and then you read six more values.
When you read the numeric values, you read them into a temporary buffer
, but then you use the input stream to assign to the structure fields.
Nowhere in the reading you handle the comma that separates the values.
As with the comma, you read the date-time without consideration of the separators in the date-time format.
You read the date-time field into separate values, then create a string of the exact same format as in the file. That's not really needed (I will tell you why below).
The date-time is stored in the structure as a string, so why are you trying to parse it, if you're just storing it in a string anyway? What, if anything, does the DateTime::Parse
function really do?
Now to help you with your problem there are a couple of things in the C++ standard library that can help you. The first is the std::getline
function, the second is std::istringstream
.
First of all I suggest you read each line from the file complete (using std::getline
), put the line into a std::istringstream
and use that for the parsing, again using std::getline
.
Something like
std::string line;
while (std::getline(in, line))
{
std::istringstream iss(line);
// Parse each line using the input string stream
}
The std::getline
by default uses the newline as separator, but it can be told to use any single character, for example ','
: Also note that I put the date-time string directly into your structure.
std::getline(iss, item->datetime, ',');
The above two lines will put the complete date-time string into the datetime
member variable. You can then get the two other values easily the same way:
std::string temp;
std::getline(iss, temp, ',');
item->open = std::stod(temp);
std::getline(iss, temp); // Last item in input, no need for separator
item->volume = std::stoi(temp);
(Read about std::stod
and std::stoi
.)
I randomly picked two member fields from your structure to initialize, because there are only two numeric fields per record in the file.