Need help in reading a .dat file, i'm unable to open the file
This program is for a Air Quality index detector, the AQI machine records the particle concentration every minute and saves it into new data file for each day. So I need to make this program run every minute and convert the concentration in to the AQI readings.
The filename is of the format "ES642_2013-11-09.dat", where ES642 is the software name of the machine, and rest is the year, month and day.
The code here is just for the file reading section:
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
int main(int argc, constchar * argv[]) {
for(;;){
time_t now = time(0);
if(now % 60 == 0) { //running the program every minute
//if(now % 1 == 0) { //running the program every second
tm *ltm = localtime(&now);
int year_int = 1900+ltm->tm_year; //defining the current year
int month_int = 1+ltm->tm_mon; //defining the current month
int day_int = ltm->tm_mday; //defining the curret day
string year = to_string(year_int); // saving them as a string
string month = to_string(month_int);
string day = to_string(day_int);
if(month.length() == 1) {
month = "0" + month;
} //since the software saves 9 as 09 in the file name, therefore modiying the month.
if(day.length() == 1) {
day = "0" + day;
} //since the software saves 9 as 09 in the file name, therefore modiying the day.
cout<<year<<"\t"<<month<<"\t"<<day<<endl;
const string filename = "ES642_" + year + "-" + month + "-" + day + ".dat";
/* for reading today's file, the filename should be
const string filename = "ES642_2013-11-09.dat";
but as there is a new data file for each day, therefore i'm using variables for each filename instead of constants
const string filename = "ES642_" + year + "-" + month + "-" + day + ".dat";
*/
string line;
ifstream myfile;
myfile.open(filename,ios::binary);
if (myfile.is_open()) {
while ( getline (myfile,line) )
{
cout << line << endl;
}
myfile.close(); }
else {
cout << "Unable to open file" << endl;
}
}
}
return 0;
}
Note what I said before:
"Your to_string calls at lines 18-20 will not format leading zeroes."
That means the the day will be formatted as 9, not 09 which will result in the string: ES642_2013-11-9.dat
That won't match the filename you said you were expecting.
ES642_2013-11-09.dat
Or wait until Sunday and it will start working until next month.