
-----------------------------------
jondmml
Tue Mar 20, 2007 8:36 am

Reading file to pointer
-----------------------------------
Hello, I was wondering how I could read a file object and place it into a pointer. Any help with this would be appreciated.

Jon

-----------------------------------
Cervantes
Tue Mar 20, 2007 10:43 am

RE:Reading file to pointer
-----------------------------------
Are you trying to read the state of an object in from a file? I don't think I understand your question. Could you please elaborate?

-----------------------------------
jondmml
Tue Mar 20, 2007 2:24 pm

Re: Reading file to pointer
-----------------------------------
I will include to explain the situation

I first create the following type in two programs

% Each moving character is represented by this type
type Character :
    record
        x, y : int                  % Location of the character
        dx, dy : int                % Direction of the character
        dir : int                   % Direction pointing of the character
        pFeature : int              % Used when calculating the mouth of Pacman
        sizex, sizey : int          % Dimensions of character
        sColor : int                % Color of character stored as integer
        pType : int                 % Types include 100. Pacman 101. Ghost
    end record

I declare a var and set atributes to it and then save it to a file. I then wish to retrieve this variable in another file and set it to apointer. Currently my code looks like this:


procedure setupPointer (pChar : ^Character, t : Character)
    pChar -> x := t.x
    pChar -> y := t.y
    pChar -> dx := t.dx
    pChar -> dy := t.dy
    pChar -> dir := t.dir
    pChar -> pFeature := t.pFeature
    pChar -> sColor := t.sColor
    pChar -> pType := t.pType
end setupPointer
var pacmanChar : pointer to Character
new pacmanChar
var t : Character
var fileNum : int
open : fileNum, "4", read
read : fileNum, t
setupPointer (pacmanChar, t)
close : fileNum

This, however, crashes turing every time it is run!

Thanks again

-----------------------------------
Cervantes
Tue Mar 20, 2007 3:25 pm

RE:Reading file to pointer
-----------------------------------
I'm not running windows, so I don't have Turing, so I can't see what the error message is. Do you think you could post that?

I've never created a pointer to a regular type like that before. Is it necessary? There are things called "collections" that you might look into. Maybe they wouldn't crash Turing.

-----------------------------------
jondmml
Tue Mar 20, 2007 3:38 pm

Re: Reading file to pointer
-----------------------------------
I've attached the panic log. I was wondering what the difference between a type and collection is and why one should be used over another.

-----------------------------------
Clayton
Tue Mar 20, 2007 4:05 pm

Re: Reading file to pointer
-----------------------------------
What is the actual error you're getting though? I ran it using my own file (by the way, there should be an extension on that filename...), and it works fine.

-----------------------------------
jondmml
Tue Mar 20, 2007 4:17 pm

RE:Reading file to pointer
-----------------------------------
Sorry about the whole confusion, I have realized my mistake. Unfortunately I have led you on a wild goose chase regarding a silly mistake where I tried to set the values inside the file to pointers which cannot work. Thanks again for your help since it helped me find my own mistake!

Jon

-----------------------------------
ericfourfour
Tue Mar 20, 2007 7:00 pm

Re: Reading file to pointer
-----------------------------------
Just a few suggestions to improve your code:

procedure setupPointer (pChar : ^Character, t : Character)
    pChar -> x := t.x
    pChar -> y := t.y
    pChar -> dx := t.dx
    pChar -> dy := t.dy
    pChar -> dir := t.dir
    pChar -> pFeature := t.pFeature
    pChar -> sColor := t.sColor
    pChar -> pType := t.pType
end setupPointer

You should think of a better name for this procedure, like setupCharacter or something similar. setupPointer can be easily confused with anything in your program that deals with pointers.

Also, this can be better suited as a function. Make it return a pointer to a character. Or even better, look into object orientation. That can definitely make your program easier to work with.
