如何在Visual C 中打开文件

How can i open a file in visual c++?

本文关键字:文件 Visual      更新时间:2023-10-16

我正在使用Visual Studio,并且有问题打开此文本文件,我将其放在文件夹中,并使用所有源代码,但我遇到了一个"无此类文件或目录"错误。这是我的代码

void Game::load_map(const char *filename)
{
    int width,height,current;
    std::ifstream in(filename);
    if(in.fail()){
        std::cout << "problem opening the file" <<std::endl;
        perror(filename);
    }
    else
    {
        in >> width;
        in >> height;
        for(int i = 0; i < height; i++)
        {
            std::vector<int> vec;
            for(int j = 0; j<width;j++)
            {
                if(in.eof())
                {
                    std::cout<< "file ends error" << std::endl;;
                    return;
                }
                in >> current;
                if(current>=0 && current<=1)
                {
                    vec.push_back(current);
                }else{
                    vec.push_back(0);
                }
            }
            map.push_back(vec);
        }
    }
    in.close();
}

这是我称之为此功能的方式:

load_map("map.map");

您编程可能不会在放置源代码的同一目录中运行,而是在SolutionDebug目录中运行。

要么将相对于此目录的文件路径传递到您的函数

load_map("..\Project\map.map");

或移动要在此处打开的文件。或第三选项,如果您不确定编程的工作目录在哪里,请提供完整的路径

load_map("c:\Blah\Blub\Project\map.map");