Problem reading from file

Hi I have a simple problem - I think.

I'm reading the content from a file into a char array:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
std::ifstream fileStream(name.c_str(), std::ios::binary | std::ios::ate);

	if (fileStream)
	{
		int length;
		//Get length of file
		fileStream.seekg(0, std::ios::end);
		length = fileStream.tellg();
		fileStream.seekg(0, std::ios::beg);
		
		char* buffer = new char[length];

		fileStream.read(buffer, length);

		return buffer;
	}


It reads the input fine, but somehow the char array is too big? The content of the file is followed by some random chars?

[EDIT]

Im really confused. Here is the content from the file copied from notepadd++:

#version 330
layout(location=0) in vec4 in_Position;
layout(location=1) in vec4 in_Color;
out vec4 ex_Color;
void main(void)
{
gl_Position = in_Position;
ex_Color = in_Color;
}

And here's what I get from the debugger:

#version 330
layout(location=0) in vec4 in_Position;
layout(location=1) in vec4 in_Color;
out vec4 ex_Color;
void main(void)
{
gl_Position = in_Position;
ex_Color = in_Color;
}ýýýý««««««««îþ

I use visual studio 2010 on a windows 7.

Is it a encoding problem?

Any help how to solve this is really appreciated :)
Last edited on
Are you sure the array is too big? How does this manifest itself?

It may be that the array is too small - depending on what you are doing, if for example you want to treat the buffer as a C-string, you need to allocate an extra byte for the null terminator, and set that byte to '\0' after reading the file data.

Without the null terminator, the processing could run on into subsequent memory bytes until some random value happens to be zero.
Last edited on
Im really confused. Here is the content from the file copied from notepadd++:

#version 330
layout(location=0) in vec4 in_Position;
layout(location=1) in vec4 in_Color;
out vec4 ex_Color;
void main(void)
{
gl_Position = in_Position;
ex_Color = in_Color;
}

And here's what I get from the debugger:

#version 330
layout(location=0) in vec4 in_Position;
layout(location=1) in vec4 in_Color;
out vec4 ex_Color;
void main(void)
{
gl_Position = in_Position;
ex_Color = in_Color;
}ýýýý««««««««îþ

I use visual studio 2010 on a windows 7.
Last edited on
Is that a true problem, or is it just an idiosyncrasy of the debugger?

I notice in the initial code, this line: return buffer;
Presumably there is a function somewhere and it returns a pointer to the buffer. However, do you also return the size of the buffer? If not, how is the rest of your program meant to know where the end of the buffer actually is?

Topic archived. No new replies allowed.