basicprogramming.org


Welcome, Guest. Please login or register.
Did you miss your activation email?
Forum time; Jul 31. 2010, 04:19
Home Help Search Calendar Login Register
News: Have you got suggestions for BasicProgramming.org? Let's hear them!
Interested in creating your own programming language? Check out the QDepartment group!

+  BASIC programming forum
|-+  Basic Coding
| |-+  General Basic Programming
| | |-+  Q&A
| | | |-+  Yabasic - Reading from files.
0 Members and 1 Guest are viewing this topic. « previous next »
Pages: [1] Go Down Reply Print
Author Topic: Yabasic - Reading from files.  (Read 419 times)
Mainchip
Guest
« on: May 19. 2008, 15:40 » Reply with quote

Hey guys I have more questions  Smiley

When opening a file:

Code:
open #1, "data.file","r"

Lets say this file contains name$, how can I print the names store name$?

Hope this makes sense.

Mainchip.
Report to moderator   Logged
Jacob
Sr. Member
****
Online Online

Posts: 414


« Reply #1 on: May 19. 2008, 18:24 » Reply with quote

I didn't really get if you just want to read in the name or if you want to store it, but either way, you will have to decide if you're storing one name per line or use "" to tell it's the beginning or end of a name. But lets say you have 1 name per line.

To read in 1 line and print and store it, you could do like this:

Code:
open #1,"data.file","r"
line input #1 name$
print name$
close #1

Was this what you meant?
Report to moderator   Logged

"The truth is rarely pure and never simple."  - Oscar Wilde
Mainchip
Guest
« Reply #2 on: May 20. 2008, 14:56 » Reply with quote

Hmm... I can't remember what I was asking for I had a few drinks last night now I cant remember.

I also came up with this rough try of storing data in a file and reading it.

Code:
#!/usr/bin/yabasic

REM Simple Database

label home
clear screen
print "Database"
print ""
print "Please state what you would like to do?"
print ""
print ""
print ""
print "1. Add a new user"
print "2. Read database"
input "" a

if (a=1) then

clear screen
open #1, "data.file","a"

input "What is the name of the new member?" name$
print ""
input "What is the email of the new member?" email$
print ""
print "1 being the least experience 5 being the most experienced in the languages."
print ""
input "Experience in HTML?" html
print ""
input "Experience in PHP?" php
print ""
input "Experience in MySQL?" mysql
print ""
input "Experience in Java?" java
print ""
input "Experience in Flash?" flash
print #1,"data ",name$,",",email$,",",html,",",php,",",mysql,",",java,",",flash
print ""
print "Member added."

wait 5
close #1
goto home

elsif (a=2) then

clear screen
stream = open("data.file","r")
while(!eof(stream))
   line input #stream txt$
   func$ = "sub sbr"+str$(sub_routine)+"$():"+txt$+":end sub"

     test$ = "sbr"+str$(sub_routine)+"$"
     compile(func$)
     result$ = execute$(name$)
     sub_routine = sub_routine + 1
wend
close #stream

print "Name:        ","E-mail:        ","HTML:    ","PHP:    ","MySQL:    ","Java:    ","Flash:"
print ""
print ""
print name$,        email$,        html,    php,    mysql,    java,    flash
else
goto home
endif

Written in the file is this:

Code:
label data
read name$,email$,html,php,mysql,java,flash

When running you have the option to "add user" then stores it in the next line of the file. So if I added myself into the "database" the file would read:

Code:
label data
read name$,email$,html,php,mysql,java,flash
data Mainchip,email@hotmail.com,1,1,1,1,1

Then running the program and choosing the option read "Database" I get an error:

Code:
---Error in ./2, line 54: syntax error at "data"

But my program should be able to read the yabasic code in the folder right?

Please tell me what is wrong here.

Mainchip.

Report to moderator   Logged
Jacob
Sr. Member
****
Online Online

Posts: 414


« Reply #3 on: May 20. 2008, 15:57 » Reply with quote

If you're storing the data as a string in an external non-.yab file, why not just skip the data and label part and just read in the string using "line input"?

on the other hand it does look like that you're trying to compile the code which poses the question, couldn't you just nake the "data.file" to a "data.yab", place "data.yab" in the lib folder inside the yabasic folder, import "data.yab" in the program, and access the data from there?

Anyway, if you do insist on doing it this way that you're doing there are a few things:

1: data is a 'protected' word in yabasic, so using it as a name for a label is not a good thing.

2: in the file the strings should be stored with "" like shown below to make them appear as strings to yabasic(or so I think), you also miss a newline before the line:

Code:
label otherNameThanData
read name$,email$,html,php,mysql,java,flash
data "Mainchip","email@hotmail.com",1,1,1,1,1

EDIT 2: This of course assumes that you didn't put a new line after the line that contains read, it wasn't there in my file, but I guess you would make a function for creating this file, and so you could make that function place the newline? Lots of options really.

and for the output you could do like this to get it like that:

Code:
print #1,"\ndata \"",name$,"\",\"",email$,"\",",html,",",php,",",mysql,",",java,",",flash

3: there also seems to be a smaller problem with your while loop for reading in data. Anyway, I assume you are going to store each person in a file of it's own, so you can do like this:

Code:
stream = open("data.file","r")
while(!eof(stream))
   line input #stream txt$
   text$ = text$+": "+txt$
wend

func$ = "sub sbr"+str$(sub_routine)+"$():"+text$+": end sub"
test$ = "sbr"+str$(sub_routine)+"$"
compile(func$)
execute$(test$)

close #stream

Basically what I do here is gather up all the lines in the file in a single variable, then compile the function, what you were doing was the compile each line on its own, I guess it would do the same, but instead of creating 3 sub routines, I'm creating one Cheesy

PS: I did put your code together all quickly and did these things, and got it working, so if you have problems with implementing this it's because I suck at explaining things  Cheesy  (Which is very likely, I've always been told that I am lol)

EDIT 1: And yes I could post the working code, but you seem to be doing this for experience and learning, so now I've just pointed out what I changed to get it running the way you described.

EDIT 3: Dear God, 3 edits? I'll just let it be now   Shocked
« Last Edit: May 20. 2008, 16:02 by Jacob » Report to moderator   Logged

"The truth is rarely pure and never simple."  - Oscar Wilde
Derek
Administrator
Hero Member
*****
Offline Offline

Posts: 1001



WWW
« Reply #4 on: May 20. 2008, 16:05 » Reply with quote

Hi,

I guess you have figured out that the error is caused by the text data items not having quotes around them. (I've replied to your PM.)

To fix that you will have add them to the "print" line when writing the data to the file.

One other thing: you are trying to compile a subroutine at runtime which can be re-written like this:
Code:
sub str0$()
data "stuff", "More Stuff", "something else", 0, 3,4,1
end sub
That will not make the data visible to the main program as data statements within a subroutine can not be read from outside that routine. IE Yabasic will throw an error if you try and execute this trivial program:
Code:
#!/usr/bin/yabasic

restore datum
read a$
print a$
sub testas()
label datum
data "whoopee"
end sub
end

One way of solving the problem is to read the data from the file like this:
Code:
dim MemberData$(1)
idx = 1
stream = open("data.file", "r")
while( ! eof(stream))
   line input #stream MemberData$(idx)
   idx = idx + 1
   dim MemberData$(idx)
wend
close #stream
It's not very neat but it works Smiley

*Shameless plug for LibIni.yab*

You could use my library for writing the data to an .ini file and use it to read the data from the file in a pseudo-random way.

Derek.

Edit: Jacob beat me to it but I'll add my answer as well.
« Last Edit: May 20. 2008, 16:08 by Derek » Report to moderator   Logged

wget http://*
Mainchip
Guest
« Reply #5 on: May 20. 2008, 16:30 » Reply with quote

Hey thanks for your replies guys but you both have lost me I fix the problem with the quotes.

Code:
print #1,"data \"",name$,"\"",",","\"",email$,"\"",",",html,",",php,",",mysql,",",java,",",flash


It's a bit of a mess  Grin.

However your right I still cannot get the data to show how can I do this again? Please explain in more detail if possible. I'm a newbie remember  Tongue

Mainchip.
« Last Edit: May 20. 2008, 16:37 by Mainchip » Report to moderator   Logged
Jacob
Sr. Member
****
Online Online

Posts: 414


« Reply #6 on: May 20. 2008, 16:37 » Reply with quote

Ok, I have the code like this:

Code:
REM Simple Database

label home
clear screen
print "Database"
print ""
print "Please state what you would like to do?"
print ""
print ""
print ""
print "1. Add a new user"
print "2. Read database"
input "" a

if (a=1) then

clear screen
open #1, "data.file","a"

input "What is the name of the new member?" name$
print ""
input "What is the email of the new member?" email$
print ""
print "1 being the least experience 5 being the most experienced in the languages."
print ""
input "Experience in HTML?" html
print ""
input "Experience in PHP?" php
print ""
input "Experience in MySQL?" mysql
print ""
input "Experience in Java?" java
print ""
input "Experience in Flash?" flash
print #1,"\ndata \"",name$,"\",\"",email$,"\",",html,",",php,",",mysql,",",java,",",flash
print ""
print "Member added."

wait 5
close #1
goto home

elsif (a=2) then

sub_routine = sub_routine + 1

clear screen
stream = open("data.file","r")
while(!eof(stream))
   line input #stream txt$
   text$ = text$+": "+txt$
wend

func$ = "sub sbr"+str$(sub_routine)+"$():"+text$+": end sub"
test$ = "sbr"+str$(sub_routine)+"$"
compile(func$)
execute$(test$)

close #stream

print "Name:        ","E-mail:        ","HTML:    ","PHP:    ","MySQL:    ","Java:    ","Flash:"
print ""
print ""
print name$,        email$,        html,    php,    mysql,    java,    flash
else
goto home
endif

Basically I only changed the things described in my previous post, everything else is the same.

And the file "data.file" like this:

Code:
label test
read name$,email$,html,php,mysql,java,flash

Then you start out by adding a member, and then you run the "Read database" from your menu, works for me anyway :p

And yes placing the " like that is a mess, but that's the escape char for " and it's the only way you'll get yabasic to print that char without using chr$() which is even messier?
Report to moderator   Logged

"The truth is rarely pure and never simple."  - Oscar Wilde
Mainchip
Guest
« Reply #7 on: May 20. 2008, 16:41 » Reply with quote

Ahhh, I understand now  Grin thanks a lot for your help, see your not that bad at explaining things if I even understood.... The 2nd time  Grin.

Mainchip.
Report to moderator   Logged
Jacob
Sr. Member
****
Online Online

Posts: 414


« Reply #8 on: May 20. 2008, 16:45 » Reply with quote

Don't thank us before it works Wink (I hope Derek is still reading along once in a while, otherwise I just referred to myself in plural)
Report to moderator   Logged

"The truth is rarely pure and never simple."  - Oscar Wilde
Mainchip
Guest
« Reply #9 on: May 20. 2008, 17:04 » Reply with quote

haha, it's not finished yet though there are still a few problems. You thought I was storing everyones "data" in a different file didn't you? When no I actually wanted to store them all in the same file one under another and when reading the data yabasic makes a list of everyone under the file.

The problem is it will only print the last added member on screen, also if you create a way to go make to the menu should I say then read the data again you get an error  Tongue.

I need to work on this lol.

EDITED: I fixed the error I forgot to put the line:

Code:
sub_routine = sub_routine + 1

But it will still only print the last added data.

Mainchip.
« Last Edit: May 20. 2008, 17:10 by Mainchip » Report to moderator   Logged
Jacob
Sr. Member
****
Online Online

Posts: 414


« Reply #10 on: May 21. 2008, 04:55 » Reply with quote

Assuming you want to keep your current way of storing data, there are 2 ways that I see you can do this, these are:

1: A single array of strings that you load all the data into.

2: Multiple arrays of strings and values that you load your data into.(Even more a mess than #1, but see the code and then see my comment on after the code itself)

The simplest is possibly option 1. You could do this like this:

Code:
sub_routine = sub_routine + 1

//Set up Array
dim Database$(7,sub_routine)

clear screen
stream = open("data.file","r")
while(!eof(stream))
   line input #stream txt$
   text$ = text$+": "+txt$
wend

func$ = "sub sbr"+str$(sub_routine)+"$():"+text$+": end sub"
test$ = "sbr"+str$(sub_routine)+"$"
compile(func$)

//Load data in to array
Database$(1,sub_routine) = name$
Database$(2,sub_routine) = email$
Database$(3,sub_routine) = str$(html)
Database$(4,sub_routine) = str$(php)
Database$(5,sub_routine) = str$(mysql)
Database$(6,sub_routine) = str$(java)
Database$(7,sub_routine) = str$(flash)

execute$(test$)

close #stream

But as you see, keeping your current data structure has a HUGE overhead(and the code doesn't look nice), so what I would suggest you did instead is to reconsider your layout in the "data.file" file, so it's something like "name email html php mysql java flash"without the label, read and data statements.(assuming you don't want to allow spaces in the name, if you want to have that you'd need to change it a tiny bit)

Because if you want to go with what I just outlined, you can get a much faster and niftier looking code, that will allow you to read any name which the associated data at any time after you read the file Cheesy

Plus you could actually get around the whole loading a sub routine from a file, compiling it and executing it on runtime, which takes up memory you can't free.
Report to moderator   Logged

"The truth is rarely pure and never simple."  - Oscar Wilde
Pages: [1] Go Up Reply Print 
« previous next »
Jump to:  
Atom RDF RSS 0.91 RSS 2.0


Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2008, Simple Machines LLC Valid XHTML 1.0! Valid CSS!