Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 text file help please (i stil need help)saving arrays of #'s
Index -> Programming, Turing -> Turing Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
skier




PostPosted: 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.
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Thu Nov 11, 2004 6:30 pm   Post subject: (No 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.
skier




PostPosted: Thu Nov 11, 2004 6:39 pm   Post subject: (No subject)

you lost me at


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




PostPosted: Thu Nov 11, 2004 6:44 pm   Post subject: (No 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.
skier




PostPosted: Thu Nov 11, 2004 6:45 pm   Post subject: (No 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)
wtd




PostPosted: Thu Nov 11, 2004 6:50 pm   Post subject: (No 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.
Mr. Glib




PostPosted: Thu Nov 11, 2004 6:50 pm   Post subject: (No 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)


wtd




PostPosted: Thu Nov 11, 2004 6:57 pm   Post subject: (No 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.
Sponsor
Sponsor
Sponsor
sponsor
skier




PostPosted: Thu Nov 11, 2004 7:52 pm   Post subject: (No 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?
wtd




PostPosted: Thu Nov 11, 2004 8:05 pm   Post subject: (No 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".
skier




PostPosted: Thu Nov 11, 2004 8:20 pm   Post subject: (No subject)

so how do i go about seting up a text file
zylum




PostPosted: Thu Nov 11, 2004 8:59 pm   Post subject: (No 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)
skier




PostPosted: Thu Nov 11, 2004 9:56 pm   Post subject: (No subject)

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




PostPosted: Thu Nov 11, 2004 10:25 pm   Post subject: (No subject)

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




PostPosted: Thu Nov 11, 2004 10:29 pm   Post subject: (No 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.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 19 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: