CLion C++ can't read/open .txt file in project directory
Asked Answered
T

6

27

I have a .txt file in my project directory that I made and populated with data.

directory structure looks like:

/Users/asd/ClionProjects/ProjectWithTemplates/
main.cpp
cmake
twoday.txt

here is my code:

#include <iostream>
#include <array>
#include <cmath>
#include <fstream>


using namespace std;

/* print array prototype */
template <size_t N>
void printArray(const array<double , N> & arr);

/* mean function prototype */
template <size_t N>
double meanArray(const array<double , N> & arr);

/* standard deviation prototype */
template <size_t N>
double sDeviation(const array<double , N> & arr);

int main() {

    string date1;
    string date2;

    array<double, 24> day1Temps;
    array<double, 24> day2Temps;
    array<double, 3> testarr = {75,70,65};

/* TESTING PURPOSES */
    printArray(testarr);
    sDeviation(testarr);
    cout << "standard deviation of array is: " << sDeviation(testarr) << endl;
    cout << "mean of array is: " << meanArray(testarr) << endl;
/* END TESTING */

    ifstream inputFile;
    inputFile.open("twoday.txt");

    if(inputFile.is_open())
    {
        inputFile >> date1;
        inputFile >> date2;

        for(int i = 1; i < day1Temps.size(); ++i)
        {
            inputFile >> day1Temps[i];
        }

        for (int j = 1; j < day2Temps.size(); ++j) {
            inputFile >> day2Temps[j];
        }
    } else cout << "File unable to open. File does not exist." << endl;


    return 0;
}

/* print array defination */
template <size_t N>
void printArray(const array<double , N> & arr){

    for(const auto & i : arr)
    {
        cout << i << " ";
    }
    cout << endl;
}

/* mean function defination */
template <size_t N>
double meanArray(const array<double , N> & arr){

    double sum;

    for (const auto & i : arr) {
        sum+=i;
    }

    return sum/arr.size();
}

/* standard deviation defination */
template <size_t N>
double sDeviation(const array<double , N> & arr){

    double mean = meanArray(arr);
    double total;

    for (const auto & i : arr){
        total+=pow(i - mean, 2);
    }
    return sqrt(total/arr.size());
}

Eveything else works fine besides my file IO. Very strange.

adding some details..............more details? :(

Topi answered 18/7, 2015 at 20:33 Comment(5)
I think to solve your problem you will have to send us your file structure...Coruscation
Perhaps your IDE sets another working directory.Detection
I'm currently looking into that. everything I have seen says that it looks inside its project directory.Topi
It is possible that the working directory of you executable is set to another location. To verify this, try giving the absolute path of your file.Studding
it says that when it builds the exe it runs it from:/Users/asd/Library/Caches/clion10/cmake/generated/1f60a9d3/1f60a9d3/Debug/Project5withTemplatesTopi
W
11

if inputFile.is_open() always returns false, inputFile.open("twoday.txt"); is not opening the file correctly, presumably because it can't find "twoday.txt"

Try setting an explicit path like "c:/path/twoday.txt" or "/path/twoday.txt" if you're using Linux. You could also try writing a file instead to see where it shows up, or something more exotic to return the current path.

Whiff answered 18/7, 2015 at 21:1 Comment(2)
I'll try writing to a file.Topi
that is exactly what is happening. Since you have the first answer I'll give it to you. ThanksTopi
D
37

Clion looks for input files and writes output files to the Debug folder. If you put your input files in there it will see them.

Donoho answered 13/10, 2015 at 8:15 Comment(0)
B
15

I'm going to presume that the working directory is being set to the path to the executable file instead of your CMakeLists.txt file.

To fix this, EITHER:

  1. put the .txt next to the executable file
  2. Explicitly set the working directory for debugging
  3. Enter the full path to the .txt file as explained in ti7's answer.
Baneberry answered 18/7, 2015 at 21:4 Comment(0)
W
11

if inputFile.is_open() always returns false, inputFile.open("twoday.txt"); is not opening the file correctly, presumably because it can't find "twoday.txt"

Try setting an explicit path like "c:/path/twoday.txt" or "/path/twoday.txt" if you're using Linux. You could also try writing a file instead to see where it shows up, or something more exotic to return the current path.

Whiff answered 18/7, 2015 at 21:1 Comment(2)
I'll try writing to a file.Topi
that is exactly what is happening. Since you have the first answer I'll give it to you. ThanksTopi
C
1

The other answers identify the issue, which really has nothing to do with CLion. When any executable starts up, it has a current working directory. By default, that is the directory where the executable lives. If the file you want to read lives anywhere else, you have to do one of three things:

  • Use a fully qualified path name for your file. Kind of a pain if you are typing in the filename at runtime.
  • Copy your data file to the executable's home. But then you'll need to copy it again when you build the release build rather than the debug build.
  • Use CLion's very nice UI to set the executable's current working directory at startup to a different value, such as your project or data folder.

I've created a brief tutorial video on how to accomplish the last: https://youtu.be/dTtZEAfh_LM

Carbonization answered 5/2, 2018 at 23:0 Comment(0)
B
0

I found out how to set CMake to build in your project directory in CLion.

  1. Go to file:Settings
  2. Now navigate to build, execution, deployment section
  3. There under build you can specify the build path relative to your project

I just set mine to ".\build" and now it always builds into my project including the file you make.

Brigantine answered 28/9, 2015 at 18:29 Comment(0)
C
0

I had the same issue:

  1. Do : Ctrl + Alt + F12 to get the full file path

  2. inputFile.open()

  3. Happy Coding

Carman answered 14/5, 2020 at 16:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.