2016-10-16 5 views
0

私はMinGW32バージョン4.9.3を使ってOpenGL4でゲームを書いています。これは、この機能が含まれています:loadShaderFile(&vs, "defaultVertexShader.glsl", GL_VERTEX_SHADER)として呼び出されたときになぜ私の文字列にランダムに置き換えられた1文字がありますか?

ここ
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); 
} 

が出力されます:

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

あなたが見ることができるように、をフルパスはすぐにfopenが呼び出されるようdefaultVertexShader.glslへの正しいパスが含まれていますが、ファイル拡張子の最初のlをランダムなASCII文字に置き換えます。実行するたびに別のlが使用されます。 stdioのバグかもしれないと思います。あなたは

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

path持っ

+2

'sizeof(path)'はあなたの文字列のサイズではありません。 – tkausl

答えて

1

は、配列が、ポインタではありませんので、sizeof(path)sizeof(const char *)を得られます。

+0

'const char * path'を' const char path [] 'に変更しました。魅力のように働いた。 – AITheComputerGuy

関連する問題