ifstream没有这样的文件或目录C ++

问题描述 投票:0回答:1

所以我试图用C ++编写类似于this的着色器类。这是我的文件结构:

| -- /source
|    | --  main.cpp
|    | -- /Shaders
|    |    | -- Shader.h
|    |    | -- shader.frag
|    |    | -- shader.vert

在我的main.cpp文件中,导入shaders.h。 Shaders.h包含shader类,该类从shader.frag和shader.vert文件中读取着色器代码(或应该如此)。我从main.cpp传递的路径是Shaders/shader.fragShaders/shader.vert,但出现错误No such file or directory

这是我(或他们)相关的着色器代码:

#ifndef SHADER_H
#define SHADER_H

#include <glad/glad.h>

#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
Shader(const char* vertexPath, const char* fragmentPath)
    {
        // 1. retrieve the vertex/fragment source code from filePath
        std::string vertexCode;
        std::string fragmentCode;
        std::ifstream vShaderFile;
        std::ifstream fShaderFile;
        // ensure ifstream objects can throw exceptions:
        vShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
        fShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
        try
        {
            // open files
            vShaderFile.open(vertexPath); <------- this is where it is getting caught
            fShaderFile.open(fragmentPath); <---------- and i assume it would be here as well
            std::stringstream vShaderStream, fShaderStream;

            // read file's buffer contents into streams
            vShaderStream << vShaderFile.rdbuf();
            fShaderStream << fShaderFile.rdbuf();

            // close file handlers
            vShaderFile.close();
            fShaderFile.close();

            // convert stream into string
            vertexCode = vShaderStream.str();
            fragmentCode = fShaderStream.str();
        }
        catch (std::ifstream::failure e)
        {
            char buffer[256];
            strerror_s(buffer, 256, errno);
            printf("ERROR::SHADER::FILE_NOT_SUCCESFULLY_READ: %s\n", buffer);
        }
...

我尝试了多种不同的路径。我也尝试通过绝对路径,并始终得到相同的错误。我真的很感谢您的帮助。

c++ file path ifstream
1个回答
0
投票

您只需要包含<fstream>库。

© www.soinside.com 2019 - 2024. All rights reserved.