Converting text (string into const char*) in C++

◇◆丶佛笑我妖孽 提交于 2019-12-13 03:28:03

问题


EDIT

I've read all recommended posts, I've tried those solutions, but non of them helped.

In short problem lies in the third argument of

glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);

This code work:

 const char *vertexShaderSource = "#version 120 \n"
                             "attribute vec3 pos;\n"
                             "attribute vec2 texCoord;\n"
                             "varying vec2 texCoord0;\n"
                             "uniform mat4 transform;\n"
                             "void main()\n"
                             "{\n"
                             "   gl_Position = transform * vec4(pos, 1.0);\n"
                             "   texCoord0 = texCoord;\n"
                            "}\0";

But I want to read it from a file, following code works

std::string s= "vertex";
std::ifstream file(s.c_str());
std::stringstream buffer;

buffer << file.rdbuf();
std::string str = buffer.str();
std::cout << str; 

And is outputing:

 #version 120
 attribute vec3 pos;
 attribute vec2 texCoord;
 varying vec2 texCoord0;
 uniform mat4 transform;

 void main()
 {
     gl_Position = transform * vec4(pos, 1.0);
     texCoord0 = texCoord;
 }

From your answers I know that I cannot just simply convert string with code like this:

 const char *vertexShaderSource = str.c_str();

And pass it into:

 glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);

So I've used following code to prevent it from ceasing to exist:

 char * writable = new char[str.size() + 1];
 std::copy(str.begin(), str.end(), writable);
 writable[str.size()] = '\0';

Passing glShaderSource(vertexShader, 1, &writable, NULL); does not work also. What else I can do?

END OF EDIT


I'am trying to rewrite a code to a function which takes file name as a parameter, and returns format accepted by glShaderSource, and somewhere I'am making silly mistake, that's the function:

processFile:

const char* processFile(const std::string fileName){

    std::ifstream file;
    file.open(fileName.c_str(), std::ios::in);

    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"  << std::endl;
  }


     const char * shaderCode = output.c_str();
     return shaderCode;

   //I've tried also:
   //  char* result = new char[output.length()+1];
   //  strcpy(result,output.c_str());
   //  return result;
 }

Function call:

const char *vertexShaderSource = processFile("./vertex"); //I am writing on linux

And bellow the code which works:

const char *vertexShaderSource = "#version 120\n"
                                 "attribute vec3 pos;\n"
                                 "void main()\n"
                                 "{\n"
                                 "   gl_Position = vec4(pos, 1.0f);\n"
                                 "}\0";



vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
glCompileShader(vertexShader);

Error messages:

0:1(1): error: syntax error, unexpected $end
0:1(1): error: syntax error, unexpected $undefined

What I'am doing wrong?

来源:https://stackoverflow.com/questions/50769270/converting-text-string-into-const-char-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!