Thread: Unable to open/read the .dat file

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    2

    Unable to open/read 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:

    Code:
    #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(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; }
    Edit


  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Things:
    1. I would suspect that the zero-adding trick you do for the day probably also has to be done for the month.
    2. It never hurts to make sure the filename you generate is what you think it is.
    3. It certainly never hurts to make sure the file you want to open exists for reals, and specifically that it exists in the directory the program lives in. If the file lives in some other directory (like your desktop, or some data directory, ...) then you need to make that path part of your filename.

  3. #3
    Registered User
    Join Date
    Nov 2013
    Posts
    2
    Quote Originally Posted by tabstop View Post
    Things:
    1. I would suspect that the zero-adding trick you do for the day probably also has to be done for the month.
    2. It never hurts to make sure the filename you generate is what you think it is.
    3. It certainly never hurts to make sure the file you want to open exists for reals, and specifically that it exists in the directory the program lives in. If the file lives in some other directory (like your desktop, or some data directory, ...) then you need to make that path part of your filename.
    Thanks for reminding me about the month. The filename being generated is same as the actual filename, plus the file & the program are in the same directory.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You could use strftime() to replace most of the code you have.
    Code:
    SYNOPSIS
           #include <time.h>
    
           size_t strftime(char *s, size_t max, const char *format,
                           const struct tm *tm);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So are you getting the "Unable to open file" message, or are you just getting no output?

    If the former, use errno (or other OS-specific things) to get the actual reason for failure to open (permissions, file not found, etc).

    If the latter, make sure there's data in the file.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unable to open file using fopen()
    By sanddune008 in forum C Programming
    Replies: 7
    Last Post: 01-13-2012, 10:46 AM
  2. Unable to open included file
    By jaisha in forum C Programming
    Replies: 12
    Last Post: 04-22-2010, 12:23 AM
  3. Unable to open include file SDL_audio.h
    By rraj.be in forum C Programming
    Replies: 2
    Last Post: 06-28-2008, 08:04 PM
  4. Unable to open a file for reading
    By raghuveerd in forum C Programming
    Replies: 16
    Last Post: 06-18-2007, 11:47 PM
  5. (Error message) Fatal: Unable to open file 'C.OBJ'
    By drojen in forum C++ Programming
    Replies: 2
    Last Post: 05-02-2006, 12:05 AM