2016-10-16 1 views
0

MinGW32 버전 4.9.3을 사용하는 OpenGL4 게임을 작성하고 있습니다. 여기 내 문자열에 임의로 대체 된 단일 문자가있는 이유는 무엇입니까?

void loadShaderFile(GLuint* shader, const char* filename, GLenum type){ 
    const char* path = "./res/shaders/"; // TODO: Proper path. 
    char* fullPath; 
    char* sourceCode; 
    long fileLength; 
    FILE* f; 

    fullPath = malloc(strlen(filename) * sizeof(char) + sizeof(path) + 1); 
    if(!fullPath){ 
     // TODO: Proper error handling. 
     fprintf(stderr, "Error: Could not allocate char* fullPath in void loadShaderFile()!"); 
     exit(EXIT_FAILURE); 
    } 
    strcpy(fullPath, path); 
    strcat(fullPath, filename); 

    printf("%s\n", fullPath); // Prints correct path. 
    printf("%s\n", fullPath); 

    f = fopen(fullPath, "rb"); // Does not open. 
    if(!f){ 
     // TODO: Proper error handling. 
     fprintf(stderr, "Error: Could not open %s in void loadShaderFile()!", fullPath); // Prints different string. 
     free(fullPath); 
     exit(EXIT_FAILURE); 
    } 
    fseek(f, 0, SEEK_END); 
    fileLength = ftell(f); 
    fseek(f, 0, SEEK_SET); 

    sourceCode = malloc(fileLength * sizeof(char) + 1); 
    if(!sourceCode){ 
     // TODO: Proper error handling. 
     fprintf(stderr, "Error: Could not allocate char* sourceCode in void loadShaderFile()!"); 
     fclose(f); 
     free(fullPath); 
     exit(EXIT_FAILURE); 
    } 
    fread(sourceCode, 1, fileLength, f); 
    *(sourceCode + fileLength) = '\0'; 

    *(shader) = glCreateShader(type); 
    glShaderSource(*(shader), 1, (char const * const *)&sourceCode, NULL); // Fucking pointers. 
    glCompileShader(*(shader)); 

    fclose(f); 
    free(sourceCode); 
    free(fullPath); 
} 

loadShaderFile(&vs, "defaultVertexShader.glsl", GL_VERTEX_SHADER)로 불리는 출력 :

./res/shaders/defaultVertexShader.glsl 
./res/shaders/defaultVertexShader.glsl 
Error: Could not open ./res/shaders/defaultVertexShader.g$sl in void loadShaderFile()! 

당신이 볼 수 있듯이이 fullpath에이 defaultVertexShader.glsl에 대한 올바른 경로를 포함하고 있지만, 빨리하면 fopen으로 불린다는이 기능을 포함 , 파일 확장자의 첫 번째 l을 임의의 ASCII 문자로 바꿉니다.이 문자는 실행될 때마다 다른 문자로 바뀝니다. stdio의 버그 일 수 있다고 생각합니다.

+2

'sizeof (path)'는 문자열의 크기가 아닙니다. – tkausl

답변

1

당신은

const char* path = ... 
fullPath = malloc(strlen(filename) * sizeof(char) + sizeof(path) + 1); 

path가 배열이 아닌,하지만 포인터, 그래서 sizeof(path)sizeof(const char *)을 얻을 것입니다.

+0

'const char * path'를'const char path []'로 변경했습니다. 매력처럼 일했습니다. – AITheComputerGuy

관련 문제