CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2010
    Posts
    2

    Angry 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!

  2. #2
    Join Date
    Mar 2010
    Posts
    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;
    }

  3. #3
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    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
  •  





Click Here to Expand Forum to Full Width

Featured