Computer Science Canada

text file help please (i stil need help)saving arrays of #'s

Author:  skier [ Thu Nov 11, 2004 6:26 pm ]
Post subject:  text file help please (i stil need help)saving arrays of #'s

i am trying to make a database and i am wondering if there is any way to save info to a file so that when you close the program it saves everything you changed, then when you reopen the database it shows the saved in fo and allows you to modify it.

Author:  wtd [ Thu Nov 11, 2004 6:30 pm ]
Post subject: 

Absolutely. This is known as a "flat file database".

The basic format is one record to a line.

Consider a piece of info about an employee for a payment system.

You have name, hours worked, and payrate. So each line in your file looks like:

code:
John Doe,40,10.50


Then when you want to get the data back, you read in the line and split it up into its three components, and store each component in the right place.

Author:  skier [ Thu Nov 11, 2004 6:39 pm ]
Post subject: 

you lost me at


"This is known as a "flat file database". "

Author:  wtd [ Thu Nov 11, 2004 6:44 pm ]
Post subject: 

Well, real relational databases (Postgres, Oracle, DB2, etc.) don't just use text files.

But you can use text files for this, as long as your data is relatively simple.

Real databases have "records", which store a bunch of info about one "thing". Text files have lines, which can function similarly.

Author:  skier [ Thu Nov 11, 2004 6:45 pm ]
Post subject: 

heres what i have:
code:


setscreen ("graphics:800,400")

var name : flexible array 1 .. 6 of string
var model : flexible array 1 .. 6 of string
var company : flexible array 1 .. 6 of string
var phone : flexible array 1 .. 6 of string
var snowday : flexible array 1 .. 6 of int
var experience : flexible array 1 .. 6 of int
var choice : int
var font1 : int
var font2 : int
var total : real
var large : int
var small : int
var delete : int
var add : int
var num : int
var aver : real
var main : string (1)
var l : string
var s : string
%var pic : int := Pic.FileNew ("pollard7.jpg")
%var picID : int := Pic.FileNew ("pollard10.jpg")
%var picID2 : int := Pic.FileNew ("skiing.jpg")
%var picID3 : int := Pic.FileNew ("pollard3.jpg")
%var picID4 : int := Pic.FileNew ("FR1203_frz03.jpg")
num := upper (name)
total := 0
large := 0
small := 100000
font1 := Font.New ("Ravie:10")
font2 := Font.New ("Ravie:28")

proc values

    name (1) := "Carl Holland"
    name (2) := "Jake Reid"
    name (3) := "Alen Kimple"
    name (4) := "Ryan Lowe"
    name (5) := "Kevin Edwards"
    name (6) := "Sam Burns"

    company (1) := "Rossignol/LINE/Armada"
    company (2) := "Head/LINE"
    company (3) := "Armada/LINE"
    company (4) := "Rossignol"
    company (5) := "Atomic"
    company (6) := "Atomic"

    model (1) := "Scratch FS/Mike Nick Pro/ARV"
    model (2) := "Mad Trix/Twelve Sixty"
    model (3) := "AR5/Twelve Sixty"
    model (4) := "Carve 5.0"
    model (5) := "S-Daddy Pimp"
    model (6) := "Beta Carve 9.1"

    phone (1) := "(905 - 668 - 8687)"
    phone (2) := "(905 - 683 - 1059)"
    phone (3) := "(905 - 683 - 5697)"
    phone (4) := "(905 - 721 - 5286)"
    phone (5) := "(905 - 896 - 6427)"
    phone (6) := "(905 - 668 - 1384)"

    snowday (1) := 67
    snowday (2) := 71
    snowday (3) := 73
    snowday (4) := 50
    snowday (5) := 2
    snowday (6) := 12

    experience (1) := 10
    experience (2) := 10
    experience (3) := 9
    experience (4) := 7
    experience (5) := 17
    experience (6) := 7

end values
%------------------------------------------------------------------------------------------
proc displayall
    color (116)
    cls
    %Pic.Draw (picID2, 480, 25, picCopy)
    locate (4, 7)
    put "Name              Fav Company              Weapon of choice               Years of experience"
    for i : 1 .. upper (name)
        locate (6 + i, 7)
        put name (i)
        locate (6 + i, 25)
        put company (i)
        locate (6 + i, 50)
        put model (i)
        locate (6 + i, 81)
        put experience (i)

    end for
    put ""
    put ""

    put "Hit a key to return to the menu"

    getch (main)

end displayall
%------------------------------------------------------------------------------------------
proc average

    color (42)
    total := 0

    for i : 1 .. upper (snowday)
        total := total + snowday (i)
    end for
    aver := total / upper (snowday)

    cls
    put "The average number of days on the hill last year was ", aver : 4 : 2
    put "Hit a key to return to the menu"
    %Pic.Draw (picID3, 200, 50, picCopy)
    getch (main)

end average
%------------------------------------------------------------------------------------------
proc mostexperience
    color (5)
    for i : 1 .. upper (experience)
        if experience (i) > large then
            large := experience (i)
            l := name (i)
        end if
    end for
    cls

    put ""
    put "The the person with the most experience is  ", l, " : ", large, " years."
    put ""
    put "Hit a key to return to the menu"

    getch (main)

end mostexperience
%------------------------------------------------------------------------------------------
proc leastexperience

    for i : 1 .. upper (experience)
        if experience (i) < small then
            small := experience (i)
            s := name (i)
        end if
    end for
    cls

    put ""
    put "The the person with the least experience is  ", s, " : ", small, " years."
    put ""
    put "Hit a key to return to the menu"

    getch (main)

end leastexperience
%------------------------------------------------------------------------------------------
proc deletestuff
    color (64)
    if upper (name) = 1 then
        cls
        put "Cannot delete item!"

    else
        loop
            cls
            for i : 1 .. upper (name)
                put i, " ", name (i)
            end for
            put ""
            put "What name would you like to delete? " ..
            get delete
            exit when delete <= upper (name) and delete > 0
        end loop
        for i : 1 .. upper (name)
            if delete = i then
                for i2 : i .. upper (name) - 1
                    name (i2) := name (i2 + 1)
                    company (i2) := company (i2 + 1)
                    model (i2) := model (i2 + 1)
                    phone (i2) := phone (i2 + 1)
                    snowday (i2) := snowday (i2 + 1)
                    experience (i2) := experience (i2 + 1)

                end for
            end if
        end for
        new name, upper (name) - 1
        new company, upper (name)
        new model, upper (name)
        new phone, upper (name)
        new snowday, upper (name)
        new experience, upper (name)

        locate (22, 36)
        put "Name #", delete, " Deleted"
        delay (2000)
    end if
end deletestuff
%------------------------------------------------------------------------------------------
proc addstuff
    color (63)
    cls
    new name, upper (name) + 1
    new company, upper (company) + 1
    new model, upper (model) + 1
    new phone, upper (phone) + 1
    new snowday, upper (snowday) + 1
    new experience, upper (experience) + 1
    put "Enter the persons name."
    get name (upper (name))
    put "Enter their favorite company."
    get company (upper (company))
    put "Enter the model they ski."
    get model (upper (model))
    put "Enter phone number (ie 905 - 678 - 4567)."
    get phone (upper (phone))
    put "Enter how many days the person skied last year."
    get snowday (upper (snowday))
    put "Enter how many yearrs of experience."
    get experience (upper (experience))

    locate (22, 36)
    put "Name added"
    delay (2000)

end addstuff
%------------------------------------------------------------------------------------------
proc showsorted
    color (54)
    cls
    var temp1 : string
    var temp2 : string
    var temp3 : string
    var temp4 : int
    var temp5 : string
    for x : 1 .. num
        for i : 1 .. num - 1
            if name (i) > name (i + 1) then
                temp1 := name (i)
                name (i) := name (i + 1)
                name (i + 1) := temp1

                temp2 := company (i)
                company (i) := company (i + 1)
                company (i + 1) := temp2

                temp3 := model (i)
                model (i) := model (i + 1)
                model (i + 1) := temp3

                temp4 := experience (i)
                experience (i) := experience (i + 1)
                experience (i + 1) := temp4

                temp5 := phone (i)
                phone (i) := phone (i + 1)
                phone (i + 1) := temp5

            end if
        end for
    end for
    for i2 : 1 .. upper (name)
        locate (2 + i2, 5)
        put name (i2)
        locate (2 + i2, 23)
        put phone (i2)
    end for
   % Pic.Draw (picID4, 400, 0, picCopy)
    locate (10,10)
    put ""..
    put ""..
    put ""..
    put "SORTED!"..
    put ""..
    put "Hit a key to return to the menu"
   
    getch (main)

end showsorted
%------------------------------------------------------------------------------------------
proc menu
    colorback (7)
    cls
    color (7)

   % Pic.Draw (pic, 20, 250, picCopy)
    %Pic.Draw (picID, 600, 50, picCopy)

    Draw.Text ("FREESKIERS", 260, 310, font2, 42)

    Draw.Text ("1) Display all riders, companies and thier weapon(s) of choice", 25, 200, font1, 50)

    Draw.Text ("2) Average number of days on the snow last year", 25, 184, font1, 50)

    Draw.Text ("3) Phone Numbers (Alphabetically) also sorts everything else", 25, 169, font1, 50)

    Draw.Text ("4) Skier with most experience", 25, 154, font1, 50)

    Draw.Text ("5) Skier with least experience", 25, 139, font1, 50)

    Draw.Text ("6) Add a skier", 25, 124, font1, 50)

    Draw.Text ("7) Delete a skier", 25, 109, font1, 50)

    Draw.Text ("8) Quit", 25, 94, font1, 50)
end menu
%------------------------------------------------------------------------------------------
values
%------------------------------------------------------------------------------------------
loop
    menu
    get choice
    %------------------------------------------------------------------------------------------
    if choice = 1 then
        displayall
    end if
    %------------------------------------------------------------------------------------------
    if choice = 2 then
        average
    end if
    %------------------------------------------------------------------------------------------
    if choice = 3 then
        showsorted
    end if
    %------------------------------------------------------------------------------------------
    if choice = 4 then
        mostexperience
    end if
    %------------------------------------------------------------------------------------------
    if choice = 5 then
        leastexperience
    end if
    %------------------------------------------------------------------------------------------
    if choice = 6 then
        addstuff
    end if
    %------------------------------------------------------------------------------------------
    if choice = 7 then
        deletestuff
    end if
    %------------------------------------------------------------------------------------------
    exit when choice = 8
    %------------------------------------------------------------------------------------------
end loop





Now how do i save things to a text file so they can be called later(when program is run next)

Author:  wtd [ Thu Nov 11, 2004 6:50 pm ]
Post subject: 

Well, looking at your code, there are a few things you should learn (or learn more about) first:


  • Records
  • Arrays
  • Case ("switch") statements


Hit the Turing Tutorials forum and do some research.

Author:  Mr. Glib [ Thu Nov 11, 2004 6:50 pm ]
Post subject: 

You'll probably want to store your info in an array of records. User defined types are going to be very handy here.

code:

type employee :
  record
  var idNum : string(5)
  var name : string(20)
  var amountofCheese : int
  end record

var x : array 1..50 of employee
var stream : int

get x(1).idNum
get x(1).name:*
get x(1).amountofCheese

open : stream , "filename.dat", write
write : stream x(1)


And your record for person 1 will be stored to a binary file.

To retrieve it and add to it...
code:

open : stream , "filename.dat", read, write, seek, mod
read : stream , x(1)

get x(2).idNum(1)
get x(2).name:*
get x(2).amountofCheese

seek : stream, * %go to end of file
write : stream, x(2)



Author:  wtd [ Thu Nov 11, 2004 6:57 pm ]
Post subject: 

Interesting approach, though a plain text serialization of the data would be rather more flexible.

Ideally, you'd use something like YAML, but writing a decent YAML interface for Turing would be an exercise in masochism.

Author:  skier [ Thu Nov 11, 2004 7:52 pm ]
Post subject: 

i have no idea what your talkin about. is there a tutorial about this stuff
or can u explain about this a little more

binary file?
record?

Author:  wtd [ Thu Nov 11, 2004 8:05 pm ]
Post subject: 

skier wrote:
i have no idea what your talkin about. is there a tutorial about this stuff
or can u explain about this a little more

binary file?


There are two kinds of files on a computer: text and binary. Both are made up of bits. That's just how computers think.

The difference is basically in how the files are terminated. A text file generally terminates when the processors runs into a byte (set of 8 bits) that's equal to zero. "Binary files" don't have that limitation.

The trouble with binary files is that you can't just open them up in a text editor and see what's going on. Text files are much more portable between different computers and different applications, and thus far more flexible.

skier wrote:
record?


A record is essentially just a set of related pieces of data that, when considered together, describe a single "thing".

Author:  skier [ Thu Nov 11, 2004 8:20 pm ]
Post subject: 

so how do i go about seting up a text file

Author:  zylum [ Thu Nov 11, 2004 8:59 pm ]
Post subject: 

code:

var fileName : string := "filename.txt"
var file : int
var s : string
open : file, fileName, put
put : file, "this will appear in the file"
close (file)

open : file, fileName, get
get : file, s : * %omit ': *' if you want to get one word at a time
put s
close (file)

open : file, fileName, get
loop
    exit when eof (file)
    get : file, s
    put s
end loop
close (file)

Author:  skier [ Thu Nov 11, 2004 9:56 pm ]
Post subject: 

how do i get it to write to the text file
is there a tutorial for this

Author:  Hikaru79 [ Thu Nov 11, 2004 10:25 pm ]
Post subject: 

Read zylum's post above. That's just what his program does.

Author:  Mr. Glib [ Thu Nov 11, 2004 10:29 pm ]
Post subject: 

wtd wrote:
Interesting approach, though a plain text serialization of the data would be rather more flexible.


True...I like have the option of dumping the entire record to the .bin file without having to write each individual component. I also like the flexibility the binary file gives you with the tell statement to keep track of where individual records are located for editing later on.

Author:  Mr. Glib [ Thu Nov 11, 2004 10:48 pm ]
Post subject: 

skier wrote:

record?


skier, a record is another data type much like arrays and variables. A record has more than one component. A variable or an array holds just one type of data (strings, boolean, int, real). A record can hold all of those types. They're really usefull when you have data of disimilar types that are related to one another. Take for instance your data base that you are planning. Each person will have a record that consists of different types of info. Lets say we are making a student database. Each student's record would be comprised of a student #, last name, first name, age, sex, grades, and so on...To have seperate arrays or variables to hold all of these pieces of info (fields) would be really cumbersome. Wouldn't it be hand to have a data type that could hold all of these? Hence the record!

code:

var stuInfo: record
   stuNum : string
   stuName: string
   stuMark :int
end record

%To assign value to the record:
stuInfo.stuNum := "123ABC"
stuInfo.stuName :="Orville"
stuInfo.stuMark :=5

%To output a record's component
get stuInfo.stuNum:*
put stuInfo.stuNum


If you want you can create your own user defined data types. Turing gives us a few but we can make more than suit our needs. In the case above, I'm probably gonna have about 1000+ students at my school. I sure as hell don't want to declare 1000 different records!!! Shocked
I could really use an array or a list of 1000 students. The problem is that I need to create a new data type. Using the "type" key word you can make your own data types! Oh yes! The power! :Mr. Glib salivates:

code:

type stuInfo:
  record
   stuNum : string
   stuName: string
   stuMark :int
  end record

var students : array 1..1000 of stuInfo

%To get info for the 1st student
get students(1).stuNum
get students(1).stuName
get students(1).stuMark


If you're not sure what an array does...well, it is a data type that holds a list of similar data. In the case above the array is holding all students data type. The (1) tells the program that the info you are typing goes in the first spot. Replace all the (1)s with (2)s and then you'll be assigning values to the 2nd spot of the array. Thus, each kid has its own spot in the array. Notice how much cleaner this is than using a bunch of related arrays?

Okay, I hope this helps...I'm going to bed! Razz

Author:  skier [ Fri Nov 12, 2004 8:54 am ]
Post subject: 

im starting to get this but i have hit a wall, in the program zylum wrote he used words to be saved in the text file. i need to do it for numbers to. i have got it to acsept variables with a single number but it wont save and array full of numbers any thoughts.

also when you create the text file it some times goes to my desktop and sometimes goes to my documents. what part of the code decides that.

Author:  Mr. Glib [ Fri Nov 12, 2004 6:09 pm ]
Post subject: 

skier wrote:
im starting to get this but i have hit a wall, in the program zylum wrote he used words to be saved in the text file. i need to do it for numbers to. i have got it to acsept variables with a single number but it wont save and array full of numbers any thoughts.


Easy...

instead of put : stream , "lotsa text"

re-write it to appear as:

code:

put : stream, nameOfvariable
put : stream, nameOfvariable2


Turing stores the data file in the same folder as your source code. If you want to change the path of this file...

code:

open : stream, "a:\goober\files\data.txt", put


Does that help?

Author:  skier [ Fri Nov 12, 2004 6:22 pm ]
Post subject: 

yep
thanks


: