Posted: Mon Jan 10, 2005 9:42 pm Post subject: Reading Text from a file
ok i know there is a tutorial on how to open a file and get like inforation, but i am lost on how i would open the file and just get the program to read every second line into an array. (BTW this is for my pacman, i have plotted in a file where the dots are going for pacman to eat, and i have 2 arrays (for the x and y location of the dots)). But i dont know how to get the program to read every second line into the y array, and the other lines into the x array.
example
25<--x
25<--y
50<--x
25<--y
how do i get it to read the x values into the x array and same for the y array.
Thank you for your help once again (to whomever decides to help me)
Sponsor Sponsor
MysticVegeta
Posted: Tue Jan 11, 2005 7:54 am Post subject: (No subject)
Simply create 2 arrays and make them 1..2 of int here is the example i made for you
Posted: Tue Jan 11, 2005 12:24 pm Post subject: (No subject)
ok i kinda changed how im doing things, i now have 2 seperate files (1 for the X Co-ordinates, and one for the Y Co-ordinates), after i get the coordinates from the files how do i draw the pac dots? cause i'd have to get an x value from the x array and then the matching y coordinate from the y array...any insight would be greatly appreciated
MysticVegeta
Posted: Tue Jan 11, 2005 5:23 pm Post subject: (No subject)
scottyrush13 wrote:
ok i kinda changed how im doing things, i now have 2 seperate files (1 for the X Co-ordinates, and one for the Y Co-ordinates), after i get the coordinates from the files how do i draw the pac dots? cause i'd have to get an x value from the x array and then the matching y coordinate from the y array...any insight would be greatly appreciated
Do you mean drawovals? cause the x's and y's will make you a box
scottyrush13
Posted: Tue Jan 11, 2005 6:06 pm Post subject: (No subject)
the x and y coordinates are the locations of where the dots will be....
Cervantes
Posted: Tue Jan 11, 2005 8:37 pm Post subject: (No subject)
You have an x array and a y array, and they both have the same lower and upper bounds (therefore they have the same number of elements.) If you read the information from your text files correctly (and if you wrote the text files correctly ) element one of the x array should match with element two of the y array. Element 2 of x array should match element 2 of y array. etc. So, to draw your dots:
code:
for i : 1 .. upper (xArray) %or upper (yArray). It doesn't matter, since they should be the same
drawdot (xArray (i), yArray (i))
end for
scottyrush13
Posted: Tue Jan 11, 2005 10:23 pm Post subject: (No subject)
lol i ended up figuring it out on my own...just had to fiddle a bit.
thanks anyways for your help...
one more question...WHO IS CERVANTES? and why does he seem to be the only person willing/able to help me?
oh well, your advice has guided me through my program so far...i thank you
scottyrush13
Posted: Wed Jan 12, 2005 12:28 pm Post subject: (No subject)
*sigh*...well i guess you guys knew this was comming, but once again that little yellow SOB wont work, (actually this has nothing to do with him, more then the dot problem)
this is what i have (im trying to read the X values into an array from a .txt file
code:
var one:int
var pacdotsx:array 1..125 of int
var pacdotsy:array 1..125 of int
open : one, "PacdotsX.txt", get
for s : 1 .. 125
get : one, pacdotsx (s)
end for
end loop
anyhelp would be appreciated, and thanks to Cervantes and MysticVegeta for their help thus far
Sponsor Sponsor
Cervantes
Posted: Wed Jan 12, 2005 7:27 pm Post subject: (No subject)
What error are you getting? I'm going to assume doing this will fix it:
code:
var one:int
var pacdotsx:array 1..125 of int
var pacdotsy:array 1..125 of int
open : one, "PacdotsX.txt", get
for s : 1 .. 125
exit when eof (one)
get : one, pacdotsx (s)
end for
You know, another way you could go about this is make a text file with a bunch of numbers in the form of a map. Then use a 2D array to store the information from the file. Then draw the stuff to the screen, based on the value of each element of the array (eg. 0 could be blank, 1 could be a dot, 2 could be pacman) and the location of that element in the array).
Posted: Wed Jan 12, 2005 10:10 pm Post subject: (No subject)
once again, i have had a question answered by cervantes...you have an answer to all questions it seems, and for that i am most thankful
scottyrush13
Posted: Thu Jan 13, 2005 11:46 am Post subject: (No subject)
cervantes, i am stuck again lol sorry!
i just have a simple question, when opening the file i get the error "Eof attempted on incompatible stream number 0."
can someone answer this....(5 bits says that cervantes is the person that'll help, hes the only one who loves me enough to help ....BUT HIS LOVE IS PURE )
Cervantes
Posted: Thu Jan 13, 2005 12:07 pm Post subject: (No subject)
I've never had that problem, personally. But I would assume that it means you're trying to do an eof on a filestream that isn't valid.
Make sure that your
code:
exit when eof (filestream)
refers to a valid filestream.
If that's not it... we'll figure it out soon
-Cervantes
scottyrush13
Posted: Thu Jan 13, 2005 12:17 pm Post subject: (No subject)
it was a simple error that my good buddy nikk..(buen) solved, the problem was that i didnt have streamin equaling anything, so it was 0, and it didnt work
code:
open : streamin, "PacdotsX.txt", get
streamin := 1
for s : 1 .. 125
exit when eof (streamin)
get : streamin, pacdotsx (s)
notice the streamin:=1? that was the problem...lol, but thank you for your help a ton cervantes......i love you!
scottyrush13
Posted: Fri Jan 14, 2005 12:13 pm Post subject: (No subject)
i have a problem again
its places dots...but not the dots i plotted! and only 5 of them...here is my program so far and please help me if you could!
Posted: Fri Jan 14, 2005 5:01 pm Post subject: (No subject)
The problem now is the same as before. You need to make your open line link to an actual file. If you have the .txt files in the same directory as your .t file, you should be O.K. here. Otherwise, you'll have to change the string. Now, by having streamin := 1 just after you open the second file, you're screwing everything up.
Try taking out both instances of the following line:
code:
streamin := 1
Or, you could change the second instance of steamin := 1 to steamin := 2. But I wouldn't recommend that.
Also, you shouldn't declare variables inside your loop. Nor should you get information from files and store them to the array, inside your loop. The reason being, the information in the file does not change. So, getting "new" information (that's not really new) each time through the loop merely slows your program down.
Lastly, carefully consider the location of the code that draws your dots.