Problem with freezing up

Blackout
01-08-2002, 10:34 AM
i am in the middle of creating a arch mage game just for fun. but i am not vary good with loops and arrays (together) + file usage! the problem is that when i go new game and i go to research(you will see it) i select a spell and the check the check box it freezes! or if i don't select a spell but i check the check box it freezes! i am so frustrated i feel like scraping the whole thing!

the other problem is when i click purchase spell i says file not found, but if i add the "\" it again freezes

if you download the file you will see what i mean! i doesn't freeze the entire computer just VB.

i would really apprechiate it if you could help me!

Blackout
01-10-2002, 09:37 AM
Can anyone figure out what i am doing wrong??

Squirm
01-10-2002, 09:59 AM
When the game locks up like this, hit Ctrl+Pause (aka Ctrl+Break) to halt execution at the current spot, so you can see what is going on. Looks to me, as I run it, like you are getting stuck in an infinite loop somewhere with no DoEvents.

The part causing it is this >>
Do While Not EOF(ASpellFile)
If AllSpells <> lstpurchase Then
Write #8, AllSpells
End If
Loop

You should not be checking the EOF while writing to a file, it just doesnt make any sense. This is what you need to sort out.

:)

Teric
01-10-2002, 10:03 AM
After a quick look at your code, I found this:


Do While Not EOF(ASpellFile)
If AllSpells <> lstpurchase Then
Write #8, AllSpells
End If
Loop


The EOF doesn't work when you are writing to a file, only when you are reading from a file. When you are writing to a file, the EOF will never be true, so you get into an endless loop. That's why it appears to freeze.

When you're writing to a file, you don't care where the file ends, because it will naturally end when you write the last record to it. So you don't need the do loop, just write out to the file.

NOTE: You can break out of an endless loop by hitting ctrl-break.

Blackout
01-10-2002, 10:37 AM
so when i am coping from the one file to the other so that it will remove the one record i don't need the EOF. i have one last question before i leave you

how can i rewrite my code so that it will work??

Squirm
01-10-2002, 10:45 AM
Why dont you just try removing the lines Do While Not EOF(ASpellFile) and Loop and see what happens.

Even better, learn to use file I/O a little better. Apart from that mistake, I saw a lot of things wrong, such as opening a file using a variable as the filehandle and then trying to close it by number. Is this your first VB game by any chance?

:confused:

Blackout
01-10-2002, 10:48 AM
I have been in and out of VB for about 1 1/2 years so i don't know everything! My code usually isn't that sloppy, but i was getting so frustrated with that Do Loop that i was trying anything and everything! LOL

PS if you count black jack and tic tac toe and a couple other cheezy games no this isn't my first game. else yes it is!

Blackout
01-10-2002, 11:07 AM
ok i have one last problem now

this is my new code

'open spellfile
testrun = FreeFile
Open App.Path & "\AllSpells.DAT" For Input As #testrun
Input #testrun, transfering
Open App.Path & "\tempspells.dat" For Output As #8
If not tranfering = lstpurchase Then
Write #8, transfering
End If
Close #testrun
Close #8
'kill original file
Kill App.Path & "\AllSpells.DAT"
'rename the temp to original
Name App.Path & "\tempspells.DAT" As App.Path & "\AllSpells.DAT"

this is my problem when i run it doen't get stuck in the loop (because it is gone ) but when i go back in and it loads the file none of the items are listed instead of just the one i wanted removed

reboot
01-10-2002, 11:12 AM
You do have to loop through the input file. i.e.

'open spellfile
testrun = FreeFile
Open App.Path & "\spells.txt" For Input As #testrun
Open App.Path & "\tempspells.dat" For Output As #8
Do While Not EOF(#testrun)
Input #testrun, transfering
If not tranfering = lstpurchase Then
Write #8, transfering
End If
Loop
Close #testrun
Close #8
'kill original file
Kill App.Path & "\AllSpells.DAT"
'rename the temp to original
Name App.Path & "\tempspells.DAT" As App.Path & "\AllSpells.DAT"

ps. you should use freefile for the output file number also

Blackout
01-10-2002, 11:14 AM
ok why do they say i don't??

Blackout
01-10-2002, 11:19 AM
ok i tried doing what you wanted but no luck i still get stuck in the loop and i turned 8 into tempfile

so this is the code now

'open spellfile
testrun = FreeFile
Open App.Path & "\AllSpells.DAT" For Input As #testrun
Input #testrun, transfering
tempfile = FreeFile
Open App.Path & "\tempspells.dat" For Output As #tempfile
If Not transfering = lstpurchase Then
Write #tempfile, transfering
End If
Close #testrun
Close #tempfile
'kill original file
Kill App.Path & "\AllSpells.DAT"
'rename the temp to original
Name App.Path & "\tempspells.DAT" As App.Path & "\AllSpells.DAT"

Teric
01-10-2002, 01:24 PM
The point is, Blackout, that you DO need to use a loop and EOF when you are reading from a file, but not when you're writing to it.

When you are writing to a file, you can put the write statement within the same loop as your read statement, if you wish, but you don't need to check EOF on a file you are writing to. Reboot's example is good, except for some typos.

Blackout
01-10-2002, 01:31 PM
know i am lost first you said their was a problem with the EOF and the Do Loop

then someone said remove it

now you say i do need it!

can you please clarify on what i have to code to get it to work correctly

is the code in reboots example write or what i am lost??

If i use reboots like i said i get stuck in the loop so...

Blackout
01-10-2002, 01:57 PM
are you saying put the loop around the input #testrun, transfering. instead of the write #tempfile, transfering???

Teric
01-10-2002, 02:42 PM
Here, Blackout. Try this code. It opens both the input and the output file before starting the loop. Then it starts the loop and continues until the end of the INPUT file. It doesn't have to check for the end of the OUTPUT file.


'open spellfile
testrun = FreeFile
Open App.Path & "\AllSpells.DAT" For Input As #testrun
tempfile = FreeFile
Open App.Path & "\tempspells.dat" For Output As #tempfile
Do Until EOF(#testrun)
Input #testrun, transfering
If Not transfering = lstpurchase Then
Write #tempfile, transfering
End If
Loop
Close #testrun
Close #tempfile
'kill original file
Kill App.Path & "\AllSpells.DAT"
'rename the temp to original
Name App.Path & "\tempspells.DAT" As App.Path & "\AllSpells.DAT"

Blackout
01-11-2002, 11:21 AM
OH MY GOD YOU ARE MY SAVIOR!!!!!!

I LOVE IT I DON"T HAVE TO REDO ALL MY CODE

(last night i rewrote my entire setup of how i could redo it to work but now i don't have to)

THANKS A LOT

Teric
01-11-2002, 12:40 PM
Blackout-

I'm glad that the code works for you. But I feel like you simply took the code I wrote and pasted it into your program. (Please correct me if I'm wrong.)

Did you understand why I did what I did? Could you do it yourself the next time? What if you wanted to read from two different files and consolidate the data into a third file?

I ask only because I feel it's more important to learn than just to grab code. If you spend all of your programming time grabbing code from others, you'll never learn how to do it yourself.

Blackout
01-11-2002, 12:59 PM
Yes You are right i did post it in my prog! But I took out my code and put it in a notepad file and put yours in a notepad file right beside it and i looked for the change! you put the input in the loop! And yes i do understand why and am not just copy and pasteing.... you put the input in the loop so that it would read the input over and over until the end of the file were as i had it, that it would read the output file. So in other words i would have never got anywere! LOL

about the 3 files i would say that i would do something like this...
(hopefully)(consolidate means copy or something right??)



firstfile = freefile
open "first.txt" for input as #firstfile
secondfile = freefile
open "second.txt" for input as #seconfile
thirdfile = freefile
open "third.txt" for output as #thirdfile
'next part tricky for me don't know what i am really to do??
'or if it is even possible the way i did it
do until EOF (firstfile) and (secondfile)
input #firstfile, strfirst
input #secondfile, strsecond
if strfirst <> strsecond then
write #thirdfile, strthird
end if
loop
close
'this will only work if the files have the same number of items.Right??

Teric
01-11-2002, 01:27 PM
A valiant effort. However, your code will not accomplish what you wanted to do. In fact, it will probably write out a blank file to 'third.txt' because your strthird variable has nothing in it.

What I originally suggested you try is to take the contents of files 1 and 2, and combine them into file 3. To eliminate the problem of both input files having a different number of items, you could 1) end the loop when one input file runs out, or 2) simply continue on with the longer file alone after the shorter file runs out. In the code below, I took option 1. Try the following:


Dim strFirst as String
Dim strSecond as String
Dim strThird as String
Dim firstfile as Integer
Dim secondfile as Integer
Dim thirdfile as Integer

'Open three files, two for input, one for output
firstfile = freefile
open "first.txt" for input as #firstfile
secondfile = freefile
open "second.txt" for input as #seconfile
thirdfile = freefile
open "third.txt" for output as #thirdfile

'Loop through both input files, and merge each set of input lines
'together to form one line of output
do until EOF (#firstfile) or EOF (#secondfile)
input #firstfile, strFirst
input #secondfile, strSecond
strThird = strFirst & strSecond
write #thirdfile, strThird
loop
close #firstfile
close #secondfile
close #thirdfile

Blackout
01-15-2002, 11:43 AM
hmmmmm.....never would have thought of that...

cool

i have always said a truely wise man is a man that says their is much he can still learn!

so i must be a wise man because i can say a million times over i can still learn A LOT MORE!!! :):)

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum