Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/128.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 从导出的文件夹中读取文件_C++ - Fatal编程技术网

C++ 从导出的文件夹中读取文件

C++ 从导出的文件夹中读取文件,c++,C++,我有一个文件(我们称之为“file.txt”),它位于文件夹/folder/where/the/file/is中。 此文件夹已导出到$folder,例如: echo$FOLDER,我得到:FOLDER/where/the/file/is 现在,我想测试文件是否存在 所以,我试过了 ifstream ifile(Name_finput); if(!ifile.good()){ cout << "File doesn't exist !" << endl;

我有一个文件(我们称之为“file.txt”),它位于文件夹
/folder/where/the/file/is
中。 此文件夹已导出到
$folder
,例如:

echo$FOLDER
,我得到:
FOLDER/where/the/file/is

现在,我想测试文件是否存在

所以,我试过了

  ifstream ifile(Name_finput);

  if(!ifile.good()){  
  cout << "File doesn't exist !" << endl;
  return;
  }
ifstream ifile(Name\u finput);
如果(!ifile.good()){

CUT< P> <代码> $文件夹<代码>不是有效的C++代码。为了访问环境变量,需要使用<代码> STD::GETEnVE()/Cuff>。下面是代码的样子:

#include <iostream>
#include <cstdlib>
#include <fstream>

int main() {

    std::ifstream ifile;

    if (const char* e = std::getenv("FOLDER")) {

        ifile.open(std::string(e) + std::string("/file.txt"));

        if (!ifile.is_open()) {

            std::cout << "File doesn't exist !" << std::endl;

        } else {

            // Do-stuff with the file

        }

    }

    return 0;
}
#包括
#包括
#包括
int main(){
std::ifstream-ifile;
if(const char*e=std::getenv(“文件夹”)){
打开(std::string(e)+std::string(“/file.txt”);
如果(!ifile.is_open()){

Std::Cuff>代码> $文件夹<代码>不是C++中的东西。您需要的是<代码> STD::GETEnVE()/Cyto>。查找<代码> GETEN()/<代码>。