Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

like image 989
Rekumaru Avatar asked Jul 18 '15 20:07

Rekumaru


People also ask

How do I open a text file in CLion?

To let CLion open a text file: You must place the file in the “cmake-build-debug” folder inside your CLion project's folder. There are two ways to accomplish this: From within CLion, in the left-hand panel area, expand the drop-downs until you see the “cmake-build-debug” folder.

How do I load a file in CLion?

In the Project tool window, right-click a file or folder, then select Deployment | Upload to from the context menu, and choose the target deployment server or server group from the list. If the default server or server group is appointed, you can also select Upload to <default deployment server or server group>.

How do I change the working directory in CLion?

If you know your CLion fairly well and don't need a demo, here's a short-hand version: Edit Configurations (from drop-down menu at top right of screen), enter the directory you want to use in Working Directory or select via "..." pop-up. Thank you so much. Link was not the correct public path to the video. Fixed now.


1 Answers

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.

like image 71
kyle san clemente Avatar answered Sep 20 '22 13:09

kyle san clemente