Board Logo
« on reading file »

Welcome Guest. Please Login or Register.
Dec 12th, 2017, 09:55am


Conforums Terms of Service | Membership Rules | Home | Search | Recent Posts | Notification | Format Your Message | Installation FAQ

Please use the forums Search feature before asking.
Please post code using the code box described in Format Your Messages.
This will keep indentation, separate it better form the message and prevent gibberish.
If the code is too long for one post or additional files are needed, upload a ZIP archive to the Just BASIC Files Archive Site.

« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: on reading file  (Read 108 times)
zzz000abc
Full Member
ImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 265
xx on reading file
« Thread started on: Dec 3rd, 2017, 9:48pm »

hi,
it seems in JB there is no command to read the file as it is !
I checked input,line input ,inputto(),input$() but failed embarassed.
for example if a file contains the following text:
'-------------------------------------------------------------------------------------
'bitmap.bas
'demonstrate the bitmap command for bmpbuttons
WindowWidth = 248
WindowHeight = 175
nomainwin
loadbmp "arrow", "bmp\arrwbttn.bmp"
loacheesybmp "blue", "bmp\bluebttn.bmp"
bmpbutton #main.button1, "bmp\blank4.bmp", [button1Click], UL, 22, 11
bmpbutton #main.button2, "bmp\blank4.bmp", [button2Click], UL, 22, 46
open "unti;tled" ,for graphics as #main
print #main,, "trapclose [quit]"
print #main, "down ; fill white; flush"

'wait here for input events
wait

[button;:1Click] 'Perform action for the button named 'button1'
print #main.button2, ,"setfocus"
print #main.button2, "bitmap arrow"
print #main.button1, "bitmap blue"
wait

[button2Click] 'Perform action for the button named 'button2'
print #main.button1, "setfocus"
print #main.button1, "bitmap arrow"
print #main.button2, "bitmap blue"
wait

[quit]
close #main
end
'------------------------------------------------------------------------
how to read it?without missing a single comma or colon or semi colon.
thank you all.
User IP Logged

bplus
Senior Member
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 1229
xx Re: on reading file
« Reply #1 on: Dec 3rd, 2017, 11:02pm »

Hi zzz,

jb20help has a nice example under LINE INPUT. It uses FILEDIALOG that allows you to select a file, then it loads it into an oversized DIM array a$(3000) way more than how many lines you might have in file. It uses LINE INPUT which reads a file line by line commas and all. Then to show a$() was loaded correctly it prints out the contents of the file loaded into the array. The index i for the array also serves the double purpose of tracking how many lines you actually do load.

Code:
  filedialog "Open","*.txt",file$   ' using OPEN dialog, looking for .txt files,
                                    '     file$ will return your selected file

  if file$="" then end              'means no file selected

  dim a$(3000)                      'dim way more lines than you might have

  open file$ for input as #f        'open the file for reading

  while not(eof(#f))                'while you haven't hit the end of file marker

    line input #f, a$(i)            'this loads whole line into a$() at index i
                                    '    which starts at 0 because not declared yet

    i=i+1    'i is incremented after line loaded so i is one ahead of lines loaded
  wend
  close #f

'show file
  for j=0 to i   '<<< you might actually have i -1 lines read into array
    print a$(j)
  next j
  end

 

I have added comments.

You should be able to read your file with this code from help.

You can load a file into a string, one really long string, but I would work with files loaded into an array of strings first, specially if you want to work with them line by line.
« Last Edit: Dec 3rd, 2017, 11:23pm by bplus » User IP Logged

B+
Rod
Administrator
ImageImageImageImageImage


member is offline

Avatar

Graphics = Goosebumps!


PM

Gender: Male
Posts: 3142
xx Re: on reading file
« Reply #2 on: Dec 4th, 2017, 04:45am »

The simplest way to get the whole file is to use the input$() command.

Code:
in$=input$(#file,lof(#file)) 
User IP Logged

zzz000abc
Full Member
ImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 265
xx Re: on reading file
« Reply #3 on: Dec 4th, 2017, 12:27pm »

thank you B+,
I think I did some mistake while checking line input command. undecided
thank you rod really simple code.
User IP Logged

zzz000abc
Full Member
ImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 265
xx Re: on reading file
« Reply #4 on: Dec 4th, 2017, 12:57pm »

here is a function to read a file into an array
Code:
fn$="bitmap.bas"
 lc=f2arr(fn$)

 for i=1 to lc
    print i;chr$(32);arr$(i)
  next



 function f2arr(fn$)
 open fn$ for input as #file
   while eof(#file)=0
    in$=input$(#file,lof(#file))
   wend
 close #file
lc=0
   while (word$(in$,lc+1,chr$(10)))<>""
     lc=lc+1
   wend
 dim arr$(lc)
 'arr$(0)=str$(lc)
   for i=1 to lc
       arr$(i)=word$(in$,i,chr$(10))
   next
   f2arr=lc
   end function
 
User IP Logged

bplus
Senior Member
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 1229
xx Re: on reading file
« Reply #5 on: Dec 4th, 2017, 4:33pm »

You are doing it the hard way and a WHILE loop is not needed if inhale the file into one string variable.
User IP Logged

B+
zzz000abc
Full Member
ImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 265
xx Re: on reading file
« Reply #6 on: Dec 4th, 2017, 8:22pm »

sorry B+ and JB forum for posting wrong code.
*I realized I should not work when I am sleepy. :-X
now the while loop is removed.
Code:
filedialog "Open","*.*",fn$   ' using OPEN dialog, looking for .txt files,
                                    '     file$ will return your selected file

  if fn$="" then end              'means no file selected
'fn$="bitmap.bas"
 lc=f2arr(fn$)

 for i=1 to lc
    print i;chr$(32);arr$(i)
  next



 function f2arr(fn$)
 open fn$ for input as #file
       in$=input$(#file,lof(#file))
    close #file
lc=0
   while (word$(in$,lc+1,chr$(10)))<>""
     lc=lc+1
   wend
 dim arr$(lc)
 'arr$(0)=str$(lc)
   for i=1 to lc
       arr$(i)=word$(in$,i,chr$(10))
   next
   f2arr=lc
   end function

 


*your code in reply #1 works faster but guessing the array size is not good idea I think.
My code on the other hand takes longer time but dim s the array to right index.
User IP Logged

bplus
Senior Member
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 1229
xx Re: on reading file
« Reply #7 on: Dec 4th, 2017, 9:27pm »

Yes, the line count is the fly in the ointment!
User IP Logged

B+
Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

Conforums Terms of Service | Membership Rules | Home | Search | Recent Posts | Notification | Format Your Message | Installation FAQ

Donate $6.99 for 50,000 Ad-Free Pageviews!

| |

This forum powered for FREE by Conforums ©
Sign up for your own Free Message Board today!
Terms of Service | Privacy Policy | Conforums Support | Parental Controls