Unable to read/open the .dat file

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main(int argc, const char * 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;
}

Last edited on
I would suggest you display filename at line 31 to make sure the string is what you're expecting to open.

You're mixing underscores and hyphens in the file name. I don't know if that what you intend.

Your to_string calls at lines 18-20 will not format leading zeroes. Also don't know if that what you intended.

What's the exact filename you're trying to open?
I just updated the post.

The filename is of the format "ES642_2013-11-09.dat", where ES642 is the software name of the AQI machine, and rest is the year, month and day.

Instead of having a constant file name i'm using a variable file name, as there is a new data file for each day.
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.
but in lines 24 - 25, i'm changing the day to 9 from 09
The main thing is that I'm using xcode for my programming and in Xcode you need to setup a scheme to get it working.....
Topic archived. No new replies allowed.