1

I am currently trying to read in a text file for use in a project but when running in release form (running the .exe file from a command window) the file doesn't read in. But yet in debug mode it works fine. I have tried having the files in the same directory as the .exe file but it doesn't seem to find it and I don't know why?

The code:

        std::cout << "Acquiring data file..." << endl;
    std::cout << "Reading data file..." << endl;
    
    ifstream tempData;
    
    tempData.open("temp_lincolnshire_short.txt");
    if (tempData.is_open()) {

        std::cout << tempData.rdbuf() << endl;
        
    }
    else {

        cout << "Reading failed..." << endl;
    }
    tempData.close();
    std::cout << "Data file closed." << endl;
  • Provide an absolute path instead of a relative path, see if that works. – Irelia May 05 '21 at 16:10
  • I'll give that a go but the application needs to be used on other pcs so the absolute path would break – Callum Godfrey May 05 '21 at 16:15
  • 1
    This is probably due to the different default directory used for debug and release builds. You need to make sure your txt file is in the correct subdirectory (the same one the exe is located in), or use an absolute path to it. – 1201ProgramAlarm May 05 '21 at 16:15
  • @CallumGodfrey I'm only stating to provide an absolute path simply as a test. Because I doubt there's an actual reason other than directories that's causing this behavior. – Irelia May 05 '21 at 16:22
  • Yeah the absolute path worked so it is the directories that's causing the issue and I have the text file in the same directory as the .exe file so I'm not entirely sure what's going wrong? – Callum Godfrey May 05 '21 at 16:29
  • VS uses different directories for debug and release modes. You must make sure the text file exists in the release folder too. – ChrisMM May 05 '21 at 16:34
  • in debug, mode file will read where vcxproj is present while in release mode file will read where exe is present.. i mean respective root directory – Roshan M May 05 '21 at 18:49

1 Answers1

0

What I ended up using was a relative path as identified on https://stackoverflow.com/a/35910234/3795116 which ended up being:

myFile.open("../Release/frequency.dat", ios::in);

*changing myFile to whatever your variable is.

JhWebDev
  • 382
  • 7
  • 13