G
G
Grigory Dikiy2016-09-15 09:39:04
linux
Grigory Dikiy, 2016-09-15 09:39:04

File not opening?

Good day! I can't open the file, tried it in different ways, but the result does not change. I develop on Linux using Clion.
Opening function:

static std::string LoadShader(const std::string& fileName)
{
    std::ifstream file;
    file.open((fileName).c_str());

    std::string output;
    std::string line;

    if(file.is_open())
    {
        while(file.good())
        {
            getline(file, line);
            output.append(line + "\n");
        }
    }
    else
    {
        std::cerr << "Unable to load shader: " << fileName << std::endl;
    }

    return output;
}

Constructor:
Shader::Shader(const std::string &filename)
{
    m_program = glCreateProgram();
    m_shaders[0] = CreateShader(LoadShader(filename + ".vs"), GL_VERTEX_SHADER);
    m_shaders[1] = CreateShader(LoadShader(filename + ".fs"), GL_FRAGMENT_SHADER);
    ......
}

Create object with opening:
Shader shader(".res/basicShader");
File structure:
4dbe6fc28790498185fac01fc51abb79.png
Cmake:
cmake_minimum_required(VERSION 3.6)
project(1_)

# includes FindSDL2.cmake
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

find_package(SDL2 REQUIRED)
find_package(OpenGL REQUIRED)
find_package(GLEW REQUIRED)

if (GLEW_FOUND)
    include_directories(${GLEW_INCLUDE_DIRS})
    link_libraries(${GLEW_LIBRARIES})
endif()

include_directories(${SDL2_INCLUDE_DIR})

set(SOURCE_FILES main.cpp src/Display.cpp headers/Display.h src/Shader.cpp headers/Shader.h)
add_executable(1_ ${SOURCE_FILES})
target_link_libraries(1_ ${SDL2_LIBRARY})
target_link_libraries(1_ ${OPENGL_LIBRARY})

Rights:
1f846600686f4608ac496555713af718.png
I would appreciate your help.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
Grigory Dikiy, 2016-09-15
@frilix

The problem was solved, it was necessary to specify the working directory in Clion.

A
Adamos, 2016-09-15
@Adamos

In C++11, the std::ifstream::open function takes a std::string as an argument.
You don't need to cripple the path with c_str translation.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question