samedi 31 octobre 2015

Converting file to string Issue in C++

I'm having a little trouble of converting a file to a string. This post is a continuation of this post.

This is the function I'm calling:

std::string fileToString(std::string const& file){

    std::ifstream in(file);
    if (!in){
        std::cout << "Error: file does not exist\n"; 

    std::stringstream buffer;
    buffer << in.rdbuf() << std::flush;

    return buffer.str();

And I called this to test it out:

std::ofstream file("test.txt");
file << fileToString("fileTest.txt").c_str();

In my scenario, I have to end up returning a char*, so in the end I want it to work like this: (where GLchar is a typedef for char in OpenGL)

const GLchar* vertexShaderSource = fileToString("Shaders\\shader.vsglsl").c_str();

However, this time, the test.txt file is empty. What happened here?

Aucun commentaire:

Enregistrer un commentaire