File Input / Output

Questions regarding syntax

File Input / Output

Postby kevinrandell » Mon Oct 31, 2011 11:17 am

I am trying to read and write to an ASCII text file to store a high scores table for a game. The code am I using is vb6 style. I cannot seem to write to a file and get a runtime error trying to open a text file and read the input. I get no syntax errors when I compile. Is there an easier way to do this?

The attachment contains the code and text files.

Regards,
Kevin
Attachments
FileInput.kbasic_project.zip
(2.71 KiB) Downloaded 134 times
kevinrandell
 
Posts: 13
Joined: Fri Jun 17, 2011 4:55 am

Re: File Input / Output

Postby Henning » Mon Oct 31, 2011 10:26 pm

Hi,

Try this.

Code: Select all
Private Sub CommandButton1_OnEvent()

  dim scores (1 to 10) as integer
  dim names (1 to 10) as string
  dim i as integer
  Dim AppPath As String = "D:\Program\KBasic\Projects\KevinRandell_FileInput\"
  Dim s As String
  Dim ss As Strings
  Dim c As Integer
  i = 1
 
  s = File.ReadText(AppPath & "Scores.txt")

  ss = Split(s, Chr(13) & Chr(10))
 
  For Each sss As String in ss
    c = InStr(sss, ",")
    If c > 0 Then
      names(i) = Left(sss, c - 1)
      scores(i) = Integer(Right(sss, Len(sss) - c))
'      Print KbCrLf & names(i) & " : " & String(scores(i))
    Label1.Caption = Label1.Caption & KbCrLf & names(i) & " : " & String(scores(i))
      i = i + 1
      End If
  Next

End Sub


/Henning
Henning
 
Posts: 262
Joined: Mon Feb 09, 2009 12:03 am
Location: Sweden

Re: File Input / Output

Postby kevinrandell » Tue Nov 01, 2011 10:40 am

Thanks, that seems to do the job.

There used to be an App.Path variable in VB6 and the default path was in the same folder as the project. Is there anyway of setting Bbasic to look first in the folder where the project is?

Cheers,
Kev
kevinrandell
 
Posts: 13
Joined: Fri Jun 17, 2011 4:55 am

Re: File Input / Output

Postby kevinrandell » Wed Nov 02, 2011 2:24 am

KBasic seems to handle things a little differently. I could not put multiple input variables and the buffer seems to read up to the comma not the whole line so this is why I could not get my previous code to run. Making the file space delimited and using the mods below I got my original code to work.

Shouldn't "input" read in the whole line, not just up to a comma?

Hard-wiring the App path is not great either, when I make a binary this will fail on other machines or accounts.

Any ideas?

Regards,
Kev

Private Sub CommandButton1_OnEvent()

dim scores (1 to 10) as integer
dim names (1 to 10) as string
dim i as integer
Dim strBuff As String
Dim AppPath As String = "/Users/kevinrandell/Documents/Introduction to Programming/Labs/FilesV2.kbasic_project/"
dim delim as integer

Label1.Caption=""
i=1

Open AppPath & "Scores.txt" For Input As #1

Do Until EOF(1)

Input #1, strBuff

delim = InStr(strBuff, " ")
If delim > 0 Then
names(i) = Left(strBuff, delim - 1)
scores(i) = Integer(Right(strBuff, Len(strBuff) - delim))
endif

if names(i)<>"" then
Label1.Caption = Label1.Caption & names(i) & " : " & str(scores(i)) & KbCrLf
endif
i=i+1

Loop
Close #1

End Sub
kevinrandell
 
Posts: 13
Joined: Fri Jun 17, 2011 4:55 am

Re: File Input / Output

Postby berndnoetscher » Wed Nov 02, 2011 8:32 am

kevinrandell wrote:Thanks, that seems to do the job.

There used to be an App.Path variable in VB6 and the default path was in the same folder as the project. Is there anyway of setting Bbasic to look first in the folder where the project is?

Cheers,
Kev


Check out
http://www.kbasic.com/doku.php?id=stati ... pplication
berndnoetscher
Site Admin
 
Posts: 1059
Joined: Tue Sep 25, 2007 9:37 am


Return to Coding Questions

cron