Thread: fseek() with stdout

  1. #1
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555

    fseek() with stdout

    This is about my snake clone again. I figure it goes here instead of Game Programming because it's more about C in general than Game specific stuff.

    Code:
            fseek(stdout, 80 * (snakepos[0][1] - 1) + snakepos[0][0], SEEK_SET);
            fscanf(stdout, "%c%c", &test[0], &test[1]);
            if ((test[0] != 32 || test[1] != 32) && (test[0] != apple[0] && test[1] != apple[1]))
                alive = 0;
    Anyway what I'm trying to do here is to go to the line where the head of the snake is, and before it moves there it gets which characters is placed there, if it's not spaces or an apple you'll die. Problem is that after fscanf, test[0] and test[1] will contain 0 (null) and I don't know why. I tried changin the position with a constant like, 3, but it still gave me zeroes.
    Am I doing something wrong or is it just not possible?

  2. #2
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    It's not possible, use gotoxy(x,y) (conio.h), SetConsoleCursorPosition(hOuput,scrn) (windows.h), or something from the curses library to position the cursor.


    And store what's on the screen in an array, it's a waste of time trying to read what is there and much easier to store it as you make it.

  3. #3
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    Oh yeah I forgot, I'm using Borland C++ on Windows.
    I can imagine using gotoxy would be a lot easier, but I don't know of any way to read in a character where the cursor is.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You don't "read in a character where the cursor is". You simply read a character. The cursor position has nothing to do with reading a character. It is an output item.

    If you want to "read the character that's currently under the cursor", then you'll need to keep track of the screen's contents in a buffer some place, and seek around through it to figure out what's under the cursor based on where you're moving it.

    If you want to make it look like the cursor it some place, and input (their typing) appears there, and you read input "from where the cursor is", like I said, you simply read input. The cursor is only an output tool. It doesn't have anything to do with reading input.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > Am I doing something wrong or is it just not possible?
    You're making life very hard for yourself.

    Code:
    char snakepit[80][25];
    Use this to keep track of the snake, food, walls etc.

    If you want to find out where something is, you look in here.

    When you make changes here, you update the screen.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fseek failing - Error: Invalid argument
    By avi2886 in forum C Programming
    Replies: 14
    Last Post: 05-14-2009, 08:49 PM
  2. Case Statement
    By danlee58 in forum C Programming
    Replies: 16
    Last Post: 11-23-2008, 08:46 PM
  3. Replies: 2
    Last Post: 07-12-2008, 12:00 PM
  4. fseek() changing an unrelated variable?
    By aaronvegh in forum C Programming
    Replies: 3
    Last Post: 11-21-2007, 02:30 PM
  5. Problems with switch()
    By duvernais28 in forum C Programming
    Replies: 13
    Last Post: 01-28-2005, 10:42 AM