Unable to 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
closed account (N36fSL3A)
So it's basically a text file with a changed extension? Are you *sure* the path is correct?
yeah, the file and the program are in the same folder.

I even tried changing the directory, but didn't work
Last edited on
duplicate here http://www.cplusplus.com/forum/beginner/116181/

Maybe you cannot open the file because the AQI machine has it open.

How are you running the program? Through an IDE? Maybe the working directory isn't the folder the exe is in.
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.....
What does the horny laptop say to the sexy desktop? .dat file
Topic archived. No new replies allowed.