1. <small id='8KB0S'></small><noframes id='8KB0S'>

      <i id='8KB0S'><tr id='8KB0S'><dt id='8KB0S'><q id='8KB0S'><span id='8KB0S'><b id='8KB0S'><form id='8KB0S'><ins id='8KB0S'></ins><ul id='8KB0S'></ul><sub id='8KB0S'></sub></form><legend id='8KB0S'></legend><bdo id='8KB0S'><pre id='8KB0S'><center id='8KB0S'></center></pre></bdo></b><th id='8KB0S'></th></span></q></dt></tr></i><div id='8KB0S'><tfoot id='8KB0S'></tfoot><dl id='8KB0S'><fieldset id='8KB0S'></fieldset></dl></div>

        <bdo id='8KB0S'></bdo><ul id='8KB0S'></ul>
      <tfoot id='8KB0S'></tfoot>
      <legend id='8KB0S'><style id='8KB0S'><dir id='8KB0S'><q id='8KB0S'></q></dir></style></legend>

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

        时间:2023-10-18
          <tbody id='S3dEt'></tbody>
      1. <tfoot id='S3dEt'></tfoot>

      2. <legend id='S3dEt'><style id='S3dEt'><dir id='S3dEt'><q id='S3dEt'></q></dir></style></legend>

          <small id='S3dEt'></small><noframes id='S3dEt'>

          • <bdo id='S3dEt'></bdo><ul id='S3dEt'></ul>
                  <i id='S3dEt'><tr id='S3dEt'><dt id='S3dEt'><q id='S3dEt'><span id='S3dEt'><b id='S3dEt'><form id='S3dEt'><ins id='S3dEt'></ins><ul id='S3dEt'></ul><sub id='S3dEt'></sub></form><legend id='S3dEt'></legend><bdo id='S3dEt'><pre id='S3dEt'><center id='S3dEt'></center></pre></bdo></b><th id='S3dEt'></th></span></q></dt></tr></i><div id='S3dEt'><tfoot id='S3dEt'></tfoot><dl id='S3dEt'><fieldset id='S3dEt'></fieldset></dl></div>
                  本文介绍了CLion C++ 无法读取/打开项目目录中的 .txt 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

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

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

                  目录结构如下:

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

                  这是我的代码:

                  #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());
                  }
                  

                  除了我的文件 IO 之外,其他一切都很好.很奇怪.

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

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

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

                  推荐答案

                  if inputFile.is_open() 总是返回 false, inputFile.open("twoday.txt"); 没有正确打开文件,大概是因为找不到 "twoday.txt"

                  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"

                  如果您使用的是 Linux,请尝试设置显式路径,例如 "c:/path/twoday.txt""/path/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.

                  这篇关于CLion C++ 无法读取/打开项目目录中的 .txt 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:freopen() 等效于 C++ 流 下一篇:在 C++ 中被 EOF 停止后如何恢复输入流?

                  相关文章

                1. <tfoot id='9WbLg'></tfoot>
                2. <legend id='9WbLg'><style id='9WbLg'><dir id='9WbLg'><q id='9WbLg'></q></dir></style></legend>

                    • <bdo id='9WbLg'></bdo><ul id='9WbLg'></ul>

                      <i id='9WbLg'><tr id='9WbLg'><dt id='9WbLg'><q id='9WbLg'><span id='9WbLg'><b id='9WbLg'><form id='9WbLg'><ins id='9WbLg'></ins><ul id='9WbLg'></ul><sub id='9WbLg'></sub></form><legend id='9WbLg'></legend><bdo id='9WbLg'><pre id='9WbLg'><center id='9WbLg'></center></pre></bdo></b><th id='9WbLg'></th></span></q></dt></tr></i><div id='9WbLg'><tfoot id='9WbLg'></tfoot><dl id='9WbLg'><fieldset id='9WbLg'></fieldset></dl></div>

                      <small id='9WbLg'></small><noframes id='9WbLg'>