C++ 从导出的文件夹中读取文件
我有一个文件(我们称之为“file.txt”),它位于文件夹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;
/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()/<代码>。