CLion C++ 无法读取/打开项目目录中的 .txt 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31495311/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 13:57:46  来源:igfitidea点击:

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

c++iofstreamreadfileclion

提问by Rekumaru

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

我的项目目录中有一个 .txt 文件,我创建并填充了数据。

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.

除了我的文件 IO 之外,其他一切都可以正常工作。很奇怪。

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

添加一些细节……更多细节?:(

采纳答案by ti7

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"

如果inputFile.is_open()总是返回falseinputFile.open("twoday.txt");则没有正确打开文件,大概是因为找不到"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.

尝试设置显式路径,例如"c:/path/twoday.txt"或者"/path/twoday.txt"如果您使用的是 Linux。您也可以尝试编写一个文件来查看它出现的位置,或者使用更奇特的东西来返回当前路径。

回答by kyle san clemente

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 查找输入文件并将输出文件写入 Debug 文件夹。如果你把你的输入文件放在那里,它会看到它们。

回答by Russell Greene

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.

我将假定工作目录被设置为可执行文件的路径,而不是您的 CMakeLists.txt 文件。

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.
  1. 将 .txt 放在可执行文件旁边
  2. 显式设置调试的工作目录
  3. 输入 .txt 文件的完整路径,如 ti7 的回答中所述。

回答by Anne Gunn

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:

其他答案确定了问题,这实际上与 CLion 无关。当任何可执行文件启动时,它都有一个当前工作目录。默认情况下,这是可执行文件所在的目录。如果要读取的文件位于其他任何地方,则必须执行以下三件事之一:

  • 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.
  • 为您的文件使用完全限定的路径名​​。如果您在运行时输入文件名,那会很痛苦。
  • 将您的数据文件复制到可执行文件的主页。但是,当您构建发布版本而不是调试版本时,您需要再次复制它。
  • 使用 CLion 非常漂亮的 UI 将可执行文件在启动时的当前工作目录设置为不同的值,例如您的项目或数据文件夹。

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

我已经创建了一个关于如何完成最后一个的简短教程视频:https: //youtu.be/dTtZEAfh_LM

回答by Timmy McCormack

I had the same issue:

我遇到过同样的问题:

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

  2. inputFile.open()

  3. Happy Coding

  1. 做: Ctrl + Alt + F12 获取完整文件路径

  2. inputFile.open()

  3. 快乐编码

回答by Ben Rasmussen

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

我发现了如何将 CMake 设置为在 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
  1. 转到文件:设置
  2. 现在导航到构建、执行、部署部分
  3. 在构建下,您可以指定相对于项目的构建路径

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

我只是将我的设置为“.\build”,现在它总是构建到我的项目中,包括你制作的文件。