-
March 9th, 2010, 10:17 PM
#1
fstream.get() - Problem
Hi! I'm trying to read a text file one character at time, but the fstream get() function read some crap after the character. Here is my code:
for (int i = 0; i < MAP_SIZE; i++)
{
for (int j = 0; j < MAP_SIZE; j++)
{
file.get(a); // reads a char
char* b = const_cast<char*>(&a); // convert to no-const
textout_ex(screen, font, b, x, y, makecol(0, 0, 0), makecol(255, 255, 255)); // print it
x += 20;
}
x = 250;
y += 20;
}
The text file is just chars, like this:
01111100000000
00000111111110
00110000000010
01100000000011
01000000000001
01100000000001
00100000000001
The code seems to work fine, but for somereason i get this:
0^1^1^1^1^1^0^0^0^0^0^0^0^0 D
^^0^0^0^0^0^1^1^1^1^1^1^1^1^0 D
0^ ^^0^1^1^0^0^0^0^0^0^0^0^1^0 D
... and continues.
Please! What i'm doing wrong?
I'm just wanna read a text file one char at time.
Thanks!
-
March 9th, 2010, 10:21 PM
#2
Re: fstream.get() - Problem
Sorry...
Code:
for (int i = 0; i < MAP_SIZE; i++)
{
for (int j = 0; j < MAP_SIZE; j++)
{
file.get(a); // reads a char
char* b = const_cast<char*>(&a); // convert to no-const
textout_ex(screen, font, b, x, y, makecol(0, 0, 0), makecol(255, 255, 255)); // print it
x += 20;
}
x = 250;
y += 20;
}
-
March 9th, 2010, 10:29 PM
#3
Re: fstream.get() - Problem
1) I assume that "a" is a char
2) I assume that textout_ex takes a char* variable that is NULL terminated.
Simply casting a char to a char * will not NULL terminate the string.
The simplest solution is probably:
Code:
char b[2] = { 0 , 0 };
file.get( &b[0] );
text_out( .... );
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|