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

clion compiler
clion cmake does not work in this environment
clion tutorial
c executable vs c library clion
clion vs visual studio
clion executable is not specified
configure clion on linux
jetbrains

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? :(

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.

Quick Tutorial: Configure CLion on Windows - Help, Alternatively, import a C/C++ project from an existing source, and CLion will create CMakeLists.txt for you. Add new files. New files can be added to the project by  For C/C++, you can set one of the predefined code styles or provide your own, and configure the desired naming convention including the header guard template: Adjust the keymap. In CLion, almost every action possible in the IDE is mapped to a keyboard shortcut. To view the default mapping, call Help | Keymap Reference.

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.

CLion: A Cross-Platform IDE for C and C++ by JetBrains, Try CLion. Cross-platform IDE for C and C++ developers. GCC and Clang You can count on the IDE for continuous analysis of your entire code base as well  CLion supports CUDA C/C++ and provides it with code insight. Also, CLion can help you create CMake-based CUDA applications with the New Project wizard. CUDA projects in CLion

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.

Start a New Project or Open an Existing - Features, You can always have multiple instances of CLion installed on the same OS, CLion needs to be provided with C and C++ compilers and the make utility. Because Clion has an extremely great speed that we need.. It becomes invaluable. I got the VisualStudio to pull through and got errors as well from 2015 saying the compiler us disfunct ,and it's not.

C & C++ Developer Tools from JetBrains, In CLion, you can use GCC and Clang compilers. On Windows, the provided environment (Cygwin, MinGW, or Microsoft Visual C++) includes the compilers  since CLion has released a month ago there aren't many documents about it. So I'm confused about how to create a c project with CLion, when I want to create a new project I just asks the name of the project and creates a default main.cpp and CMakeLists.txt file which refers to main.cpp file.

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.

Quick Tutorial: Configure CLion on macOS - Help, Download the latest version of CLion for Windows, macOS or Linux. Buy CLion: Get JetBrains Toolbox subscription for an Organization, for Individual Use or view Special Offers.

Switching Compilers - Help, In this video, I show you how to install and setup CLion so that you can successfully begin to code in C and C++. We install MinGW to allow CLion to compile C and C++ programs.

Download CLion: A Smart Cross-Platform IDE for C and C++, CLion supports GCC and Clang compilers. This means that on Windows you can select between MinGW (or MinGW-W64) and Cygwin  environments, Microsoft Visual C++ compiler (mind, the debugger is not available in this case) or Windows Subsystem for Linux toolchain.

How to Setup CLion for C and C++ Development, CLion is a cross-platform IDE for C and C++ by JetBrains. https://www.jetbrains.com/clion/

Comments
  • I think to solve your problem you will have to send us your file structure...
  • Perhaps your IDE sets another working directory.
  • I'm currently looking into that. everything I have seen says that it looks inside its project directory.
  • 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.
  • it says that when it builds the exe it runs it from:/Users/asd/Library/Caches/clion10/cmake/generated/1f60a9d3/1f60a9d3/Debug/Project5withTemplates
  • I'll try writing to a file.
  • that is exactly what is happening. Since you have the first answer I'll give it to you. Thanks