Reading ASCII files
I have the following code (modified from a post) that compiles fine. however, no data is read on execution and no print out is sent to the screen. I am not sure if teh file i have specified is even being recognised and read.
can anyone spot where i have gone wrong here - been on it for ages (just cant get it to work...)
Any help greatly appreciated.
J
Code below:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <vector>
using namespace std;
// A structure used to store time
struct Date {
unsigned int year;
unsigned int month;
float day;
};
int main()
{
// STEP 1: READ THE FILE
ifstream file_to_read;
file_to_read.open("AAPL.txt");
const int max_num_of_char_in_a_line = 512,// Maximum number of characters expected in a single line in the header
num_of_header_lines = 0; // Number of header files to skip
// STEP2: SKIP ALL THE HEADER LINES
for(int j = 0; j < num_of_header_lines; ++j)
file_to_read.ignore(max_num_of_char_in_a_line, '\n');
// STEP3: Create the ARRAYS
vector<Date> date_array;
vector<float> open_array, high_array, low_array, close_array, volume_array, openinterest_array, tot_volume_array, tot_openinterest_array;
while(!file_to_read.eof())
{
Date date_read;
file_to_read >> setw(4) >> date_read.year;
file_to_read >> setw(2) >> date_read.month;
file_to_read >> date_read.day;
date_array.push_back(date_read);
float open, high, low, close, volume, openinterest, tot_volume, tot_openinterest;
file_to_read >> open >> high >> low >> close >> volume >> openinterest >> tot_volume >> tot_openinterest;;
open_array.push_back(open);
high_array.push_back(high);
low_array.push_back(low);
close_array.push_back(close);
volume_array.push_back(volume);
openinterest_array.push_back(openinterest);
tot_volume_array.push_back(tot_volume);
tot_openinterest_array.push_back(tot_openinterest);
}
// STEP4: USE THE DATA READ AND STORED IN THE ARRAY HOW EVER YOU WANT
cout << "Dates read are:\n";
for(vector<Date>::size_type j = 0; j < date_array.size(); ++j)
cout << date_array[j].year << '/' << date_array[j].month << '/' << date_array[j].day << '\n';
cout << "Close prices are:\n";
for(vector<float>::size_type j = 0; j < close_array.size(); ++j)
cout << close_array[j] << '\n';
return 0;
}

