Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/137.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++ 使用ifstream打开文件时遇到问题_C++ - Fatal编程技术网

C++ 使用ifstream打开文件时遇到问题

C++ 使用ifstream打开文件时遇到问题,c++,C++,我需要一些关于打开文件和解析文件的帮助,但是我遇到了一个问题,ifstream没有打开文件。正在使用扩展名正确传递文件名 问题在于myFile.open(file)似乎没有“实际打开”该文件,因为我得到常数无法打开文件输出 已编辑代码 原因:我注意到它在检查文件时没有使用完整的文件路径;它现在可以正确地识别文件所在的位置,但是仍然无法打开它们 以下是我的工作内容: #include "Parser.h" using namespace std; Parser::Parser() {}; v

我需要一些关于打开文件和解析文件的帮助,但是我遇到了一个问题,
ifstream
没有打开文件。正在使用扩展名正确传递文件名

问题在于
myFile.open(file)似乎没有“实际打开”该文件,因为我得到常数
无法打开文件
输出

已编辑代码 原因:我注意到它在检查文件时没有使用完整的文件路径;它现在可以正确地识别文件所在的位置,但是仍然无法打开它们

以下是我的工作内容:

#include "Parser.h"

using namespace std;

Parser::Parser() {};

void Parser::parseFile(std::string dir, const char* file)
{
    dir = dir + "\\" + std::string(file);
    cout << dir;
    //cout << dir;
    ifstream myFile;

    myFile.open(file);
    if (! myFile)
    {
        cout << "Could not open file " << myFile <<endl;
        //exit(3);
    }
}
#包括“Parser.h”
使用名称空间std;
Parser::Parser(){};
void Parser::parseFile(std::string dir,const char*文件)
{
dir=dir+“\\”+std::string(文件);
不能包含“Parser.h”
使用名称空间std;
Parser::Parser(){};
void Parser::parseFile(std::string dir,const char*文件)
{
dir=dir+“\\”+std::string(文件);

您是否可以检查文件的chmod属性?该文件是否存在?您是否具有读取该文件的适当权限?ifstream myFile(file,ifstream::in);您可以使用特定于操作系统的函数来确定问题(如Unix的peror())@托马斯:不需要添加
ifstream::in
,因为
ifstream
构造函数总是将
ios\u base::in
添加到模式中。
#include "Parser.h"

using namespace std;

Parser::Parser() {};

void Parser::parseFile(std::string dir, const char* file)
{
    dir = dir + "\\" + std::string(file);
    cout << dir;
    //cout << dir;
    ifstream myFile;

    myFile.open(file);
    if (! myFile)
    {
        cout << "Could not open file " << myFile <<endl;
        //exit(3);
    }
}