Skip to main content
GameDev.net gamedev.net
🔒 Locked

fread string lengths problem

Started by Glidisen Mar 11, 2005 at 10:34 AM 5 replies 6.7k views
Original Post
Glidisen
Glidisen
Hey, Heres some code...

char * buffer1;

fread(&tsize, sizeof(int), 1, pIn); //Reads 7, its a print of how long...
//...the string is. The string is "FontMap".

buffer1 = (char*) malloc (tsize); //Also tried buffer1 = new char[tsize];

fread(buffer1, 1, tsize, pIn); //Read in 7 bytes into buffer1?


Now when i check the buffer1 value... + buffer1 0x003c4610 "FontMap««««««««þîþîþîþîþ" char * Whats with the "««««««««þîþîþîþîþ"? Thanks.
paulecoyote
paulecoyote
if you don't read in the null terminating character into your buffer, outputting trace (depending how you did it) will read until either it overflows or flukes into a null terminating character.

f 1
o 2
n 3
t 4
m 5
a 6
p 7
--- Your reading thing stops here

/0 8


You could cheat by using ZeroMemory on that buffer before using it. If you are using C++ and not C then using the new operator is preferred to malloc and related functions.

Also take care that a character could be more then one byte, for example if it's unicode.
Anything posted is personal opinion which does not in anyway reflect or represent my employer. Any code and opinion is expressed “as is” and used at your own risk – it does not constitute a legal relationship of any kind.
Glidisen
Glidisen
Thanks for the reply, but i still dont understand.

What is the null terminator?
Do i need the "/0" in my file?

Talk to me like im 3 years old. ;)
kdogg
kdogg
In C, strings are null terminated character arrays. That is, they are a block of sequential characters in memory followed by the null character. When you print out a string, you are giving the print function a pointer to the first character in the string. The print function will print that character and ever successive character in memory until it reaches the null character, at which time it will return. If you do not insert the null character, the print function (as well as other string manipulation functions) will not know where to stop and will proceed to print out random data in memory.

For every string, you must have a character array that is one longer than the size of the string you want to store in it (assuming ascii encoding). If your code looked like this:

char array[] = "FontMap"

The variable 'array' would be 8 characters long and hold these values:

array[0] = 'F'
array[1] = 'o'
array[2] = 'n'
array[3] = 't'
array[4] = 'M'
array[5] = 'a'
array[6] = 'p'
array[7] = '\0'
Glidisen
Glidisen
Ahh, thanks paulecoyote and kdogg.


	fread(&tsize, sizeof(int), 1, pIn);	buffer1 = new char[tsize];	buffer1[tsize] = '\0'; //Nullie	fread(buffer1, 1, tsize, pIn);
kdogg
kdogg
Close, but you just put a null character past your buffer. You really want to allocate tsize+1 bytes and then put a null in tsize.

Glidisen
Glidisen
Hmm, tsize + 1 is 9 chars(0 - 8).

It may be ugly? but it works! =)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.