Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Reading Text from a file
Index -> Programming, Turing -> Turing Help
Goto page 1, 2, 3, 4  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
scottyrush13




PostPosted: 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
Sponsor
sponsor
MysticVegeta




PostPosted: 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


IO.zip
 Description:

Download
 Filename:  IO.zip
 Filesize:  334 Bytes
 Downloaded:  127 Time(s)

scottyrush13




PostPosted: 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




PostPosted: 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 Confused
scottyrush13




PostPosted: 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




PostPosted: 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 Wink) 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




PostPosted: 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 Very Happy
scottyrush13




PostPosted: 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
Sponsor
sponsor
Cervantes




PostPosted: 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).

scottyrush13 wrote:

WHO IS CERVANTES?

He wrote Don Quixote. Smile http://www.donquixote.com/english.html
scottyrush13




PostPosted: 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 I am not worthy
scottyrush13




PostPosted: 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 Sad ....BUT HIS LOVE IS PURE Very Happy )
Cervantes




PostPosted: 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 Smile
-Cervantes
scottyrush13




PostPosted: 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! Laughing
scottyrush13




PostPosted: Fri Jan 14, 2005 12:13 pm   Post subject: (No subject)

i have a problem again Sad

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!



PacdotsY.txt
 Description:
y array

Download
 Filename:  PacdotsY.txt
 Filesize:  601 Bytes
 Downloaded:  154 Time(s)


PacdotsX.txt
 Description:
x array

Download
 Filename:  PacdotsX.txt
 Filesize:  623 Bytes
 Downloaded:  169 Time(s)


PACMAN.t
 Description:
main program

Download
 Filename:  PACMAN.t
 Filesize:  2.95 KB
 Downloaded:  152 Time(s)

Cervantes




PostPosted: 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.

-Cervantes
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 4  [ 50 Posts ]
Goto page 1, 2, 3, 4  Next
Jump to:   


Style:  
Search: