So I'm doing some work with openGL, and when it comes to writing some code for turning a file into a c string I came across a question.
[CODE]const GLchar* buildShader(const char * input){
GLchar c;
int i = 1;
sContext = fopen(input, "r");
shader = malloc(_MAX_PATH);
while (fscanf(sContext, "%c", &c) != EOF){
shader[i-1] = c;
i++;
}
shader[i] = "\0";
return shader;
}[/CODE]
now the problem is my result in shader is this
[CODE]#version 150
out vec4 outColor;
void main()
{
outColor = vec4(1.0, 1.0, 1.0, 1.0);
}ÍxÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍýýýý««««««««[/CODE]
Shouldn't the memory allocated be null chars or something along that line? Just curious i think I'm just going to go with a static array anyways but i would like to know.
the problem is your string is not null terminated.
You tried to terminate it with a "\0", but remember "" is for strings. The null terminating char is '\0'.
So it should be : shader[i] = '\0';
[QUOTE=acarvromvroom;46807939]the problem is your string is not null terminated.
You tried to terminate it with a "\0", but remember "" is for strings. The null terminating char is '\0'.
So it should be : shader[i] = '\0';[/QUOTE]
Wow I'm a dummy, its always the quotes.
I don't really have a clue whats going on in your code what so ever.
You have a variable called "shader", which doesn't seem to be declared locally, and there doesn't seem to be a reason for it to be global.
Shader is assigned to malloc(_MAX_PATH), so instead of figuring out how big the file is somehow just assign it to a static size, and limit the max the length of the content of your shader file to 260 characters.
Instead of using read, or fread, or even fgetc, you use fscanf in a for loop, while only requesting a single character at a time?
You assign "i" to be 1, but then use it to access the array by doing, "i-1", why not just assign i to 0 and access the array using "i"?
Using a static array won't solve your problem, it breaks multithreading compatibility, and you shouldn't allocate large pieces of data on the stack, especially when you don't [B]need[/B] it to be static. Don't use static content as a way to avoid having to have the caller call free on the memory you allocate for it.
You also forget to close the file after reading from it.
The only thing you're trying to do is load a file into memory, if you just have a google at "load file into memory C" you'll get a thousand of examples.
An pretty much standard C implementation would look something like this:
[cpp]
/*
Returns amount of bytes read, or <0 when reading failed.
*/
int lftm(const char *filename, char **result)
{
int size = 0;
FILE *f = fopen(filename, "rb"); // Try to open the file
if (f == NULL) // Check if opening the file succeeded
{
*result = NULL;
return -1; // Opening file failed
}
fseek(f, 0, SEEK_END); // Set the pointer to the end of the file
size = ftell(f); // Request the position of the pointer
fseek(f, 0, SEEK_SET); // Set the pointer to the beginning of the file again
*result = (char *)malloc(size + 1); // Allocate a buffer that can contain the whole file, plus an trailing NULL
if (size != fread(*result, sizeof(char), size, f)) // Try to read the file, if amounts of bytes read was not equal to amount expected then
{
fclose(f)
free(*result);
return -2; // Reading file failed
}
fclose(f); // Close the file, so other programs can access it
(*result)[size] = 0; //Set the last byte to be 0
return size;
}[/cpp]
Sorry, you need to Log In to post a reply to this thread.