Stumped(VB 6 Question)

Started by
7 comments, last by Dim_Yimma_H 19 years, 6 months ago
I'm completely stumped.. i'm working on a school final project for my final year.. And i'm having some trouble with some very simple code.. I'm sure it's going to hit me in the head once it's figured out. I'm making a chat program that only works on one networked folder, it sends data to the text file and receives the data from it constantly. If the data in the text file is not the same as the output window, then it adds it to the output window. The problem is, when i run multiples of this program(2) it ends up saying input past end of file. I know what that error means, but i cant figure out why it would say that. Thanks alot in advance for the help that is given.. here is my code. Public myname$ Private Sub cmdSend_Click() Open App.Path + "/text.txt" For Output As #1 Write #1, myname$ & " says: " & Text1.Text Close #1 Text1.Text = "" End Sub Private Sub Command1_Click() MsgBox (myname$) End Sub Private Sub Form_Load() myname$ = InputBox("What is your name?") Open App.Path + "/text.txt" For Output As #1 Write #1, "Please refrain from using profanity." Close #1 Text1.Text = "" End Sub Private Sub Form_Terminate() Open App.Path + "/text.txt" For Output As #1 Write #1, "" Close #1 Text1.Text = "" End Sub Private Sub Form_Unload(Cancel As Integer) Open App.Path + "/text.txt" For Output As #1 Write #1, myname$ & " says: " & Text1.Text Close #1 End Sub Private Sub Text1_KeyPress(KeyAscii As Integer) Open App.Path + "/text.txt" For Output As #1 Write #1, myname$ & " says: " & Text1.Text Close #1 End Sub Private Sub Timer1_Timer() listshow.ListIndex = listshow.ListCount - 1 Open App.Path + "/text.txt" For Input As #1 Input #1, a$ Close #1 If listshow.Text <> a$ Then listshow.AddItem (a$) End If End Sub
Advertisement
You're probably having locking problems. When one program wants to write to the file it is locked and writing occurs. Depending on the type of lock, other programs may or may not be able to read the file. I'm guessing what's happening with you is that the file is write-locked and cannot be read, or two programs are trying to write to the file at the same time. You may wish to look into using sockets, named pipes or other inter-program communication methods as the one you're trying isn't very scalable (imagine 20 people using it at once).
It probably happens because you always use 1 for the file descriptor. A better method would be to use the FreeFile function to get a unique and legitimate number. Example:

FileNum = FreeFileOpen "c:\text.txt" For Input As FileNumInput #FileNum, a$Close #FileNum


You might also want to consider using Scripting.FileSystemObject instead of the Open/Close method.
I think the problem lies in the Private Sub Timer1_Timer(). It could be wise to check if the end of the file has been reached to not input past it. You can use the EOF(file number) function (End Of File). If it returns true the end of the file has been reached or it contains no data.

'something like this:Private Sub Timer1_Timer()listshow.ListIndex = listshow.ListCount - 1Open App.Path + "/text.txt" For Input As #1If EOF(1) = FalseInput #1, a$ 'now a will only recieve the data if there is data left to recieveEnd IfClose #1


\Jimmy H
Here's what i -think- the problem might be.

When the text file appears, the document contains the data, but it also holds another blank line in it. I think when i'm inputting into string a, it's also pulling in that blank line, which is past the first line which is being read.

I just dont know why it's adding the line, or if in fact that is the problem at all.

I did this project last year, and i had it working perfectly, i'm just missing something really stupid.
Jimmy, the eof line gives me a syntax error.. Is there something i should be doing to add this command in?
Quote:Original post by Merics
Jimmy, the eof line gives me a syntax error.. Is there something i should be doing to add this command in?


If EOF(1) = False Then
I changed it to,
Private Sub Timer1_Timer()
listshow.ListIndex = listshow.ListCount - 1
Open App.Path + "/text.txt" For Input As #1
If EOF(1) = False Then
Input #1, a$ 'now a will only recieve the data if there is data left to recieve
End If
Close #1

End Sub

And it doesnt give me the syntax anymore, but it's not working either. I also believe that the problem resides in the timer part. Also, i noticed throughout the project i used WRITE.. Should i be using PRINT? In any case, when i flip flop them, i still get the same errors when i run the program
Haha i forgot the Then keyword (been doing too much C++ lately)

If EOF(1) = False Then 'I forgot this one sorry :)Input #1, a$End If


\Jimmy H

This topic is closed to new replies.

Advertisement