为什么我的字符串中随机替换了一个字符?
Why is there a single character replaced randomly in my string?
我正在使用 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) = '[=10=]';
*(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 中的错误。
你有
const char* path = ...
fullPath = malloc(strlen(filename) * sizeof(char) + sizeof(path) + 1);
path
不是一个数组,而是一个指针,所以 sizeof(path)
会产生 sizeof(const char *)
.
我正在使用 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) = '[=10=]';
*(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 中的错误。
你有
const char* path = ...
fullPath = malloc(strlen(filename) * sizeof(char) + sizeof(path) + 1);
path
不是一个数组,而是一个指针,所以 sizeof(path)
会产生 sizeof(const char *)
.