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

Username:   Password: 
 RegisterRegister   
 [Tutorial] File I/O
Index -> Programming, Turing -> Turing Tutorials
Goto page Previous  1, 2, 3  Next
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Tony




PostPosted: Sun Feb 03, 2008 2:05 pm   Post subject: RE:[Tutorial] File I/O

If you write to the file that doesn't exist, the system will create one for you.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Sponsor
Sponsor
Sponsor
sponsor
anna12345




PostPosted: Thu Apr 24, 2008 10:25 am   Post subject: Re: [Tutorial] File I/O

what is filenumber exactly?
riveryu




PostPosted: Sat May 31, 2008 10:54 pm   Post subject: RE:[Tutorial] File I/O

I think filenumberer is the ID given to the file. You have more than one file open and you refer to a specific by its ID. Just as using pictures, you have a pic ID.
PesticideUse




PostPosted: Sun Aug 24, 2008 10:21 pm   Post subject: Re: [Tutorial] File I/O

Hey, hopefully someone can help.
Im trying to save 14 variables, i know how to, just something is wrong.
when i go to save an error occurs, "I/O attempted on unopened stream number -10. Opened with fail message 'File "C:\File.txt" is already open (may not be opened twice)' but when i open the text document it has saved the first four lines i need Confused

i have a section of code above that loads the info that could be the problem but i put close(streamNumber) and close : streamNumber underneath so that should close the file right? I'm probably doing something wrong i don't know... if anyone has a clue please help, and if you need the code ill post the game
riveryu




PostPosted: Mon Aug 25, 2008 11:26 am   Post subject: RE:[Tutorial] File I/O

That's suppose to be in the help section. Either some mod move or it or you start another topic in Help.

Anyways. Whats the four lines that you need? Are they the first 4 variables?
If you get an error like that its usually something wrong with your loop. Make sure your open statement is not in the loop you use to write, but I doubt you made this mistake.

Post some code with your topic in Help.
PesticideUse




PostPosted: Mon Aug 25, 2008 6:12 pm   Post subject: Re: [Tutorial] File I/O

Omg im dumb, wrong forum and the simplest answer ever, sorry about that everyone
TheFerret




PostPosted: Sat Oct 11, 2008 3:04 pm   Post subject: Re: [Tutorial] File I/O

Added to wiki: Turing_File_IO
FreshAir




PostPosted: Wed Jan 21, 2009 7:52 pm   Post subject: Re: [Tutorial] File I/O

You should also add how to write to a file in another directory.
In turing this is done by doing a double backslash (//) instead of a single backslash (/)
ex.
Turing:
var fileName : string := "C:\\MyDocuments\\RandomFolder\\TheTextFile.txt"


Mod Edit: Remember to use syntax tags! Thanks Smile
code:
[syntax="turing"]Code Here[/syntax]
Sponsor
Sponsor
Sponsor
sponsor
uknowhoiam




PostPosted: Tue May 12, 2009 6:49 pm   Post subject: RE:[Tutorial] File I/O

thank you for the tutorial, i know how to input but im having trouble with output... here is my game and i have saved the score to a text file, now i want it to show up on the screen

Turing:
import GUI
View.Set ("graphics:300;400,offscreenonly")

var ballx, bally, xpos, xpos2, ypos, gravity : real
var best, line, Colour, face, brick_colour, brickx, bricky, brickx2, bricky2, brickx3, bricky3, brickx4, bricky4, brickx5, bricky5, brickx6, bricky6, brickx7, bricky7, brickx8, bricky8, dotx, doty,
    dotx2,
    doty2, dotx3, dotx4, doty3, doty4, doty5, doty6, doty7, doty8, score, picID, destination, speed, level : int
var keys : array char of boolean
var fail, die, save : boolean := false
var Score : boolean := true
var fontID, height, ascent, descent, internalLeading : int
var rec : int
open : rec, "Happy Ball.txt", put

Colour := 119
face := 255
line := 50
ballx := line + maxx - 100
bally := line
xpos := 1
xpos2 := -1
ypos := 8
gravity := .25
brickx := Rand.Int (0, 250 - 40)
bricky := Rand.Int (50, 90)
brickx2 := brickx + 40
bricky2 := bricky + 15
brickx3 := Rand.Int (0, 250 - 40)
bricky3 := Rand.Int (130, 200)
brickx4 := brickx3 + 40
bricky4 := bricky3 + 15
brickx5 := Rand.Int (0, 250 - 40)
bricky5 := Rand.Int (245, 275)
brickx6 := brickx5 + 40
bricky6 := bricky5 + 15
brickx7 := Rand.Int (0, 250 - 40)
bricky7 := Rand.Int (305, 350)
brickx8 := brickx7 + 40
bricky8 := bricky7 + 15
brick_colour := brown
dotx := maxx div 4
doty := 10
dotx2 := dotx + 3
doty2 := doty + 15
dotx3 := maxx div 2 + dotx
dotx4 := dotx3 + 3
doty3 := 110
doty4 := doty3 + 15
doty5 := 210
doty6 := doty5 + 15
doty7 := 310
doty8 := doty7 + 15
score := 0
destination := 500
speed := 17
level := 1


colourback (103)
cls


procedure Player
    drawfilloval (round (ballx), round (bally), 10, 10, Colour)     %body
    drawoval (round (ballx), round (bally), 10, 10, face)     %body outline
    drawfilloval (round (ballx) - 3, round (bally) + 2, 1, 1, face)     %left eye
    drawfilloval (round (ballx) + 3, round (bally) + 2, 1, 1, face)     %right eye
    drawfillarc (round (ballx), round (bally) - 2, 2, 4, 180, 360, face)     %smile
end Player

procedure background
    colourback (103)
    drawfillbox (dotx, doty, dotx2, doty2, black)
    drawfillbox (dotx, doty3, dotx2, doty4, black)
    drawfillbox (dotx, doty5, dotx2, doty6, black)
    drawfillbox (dotx, doty7, dotx2, doty8, black)
    drawfillbox (dotx3, doty, dotx4, doty2, black)
    drawfillbox (dotx3, doty3, dotx4, doty4, black)
    drawfillbox (dotx3, doty5, dotx4, doty6, black)
    drawfillbox (dotx3, doty7, dotx4, doty8, black)
end background

procedure game
    loop
        background
        drawfillbox (brickx, bricky, brickx2, bricky2, brick_colour)
        drawfillbox (brickx3, bricky3, brickx4, bricky4, brick_colour)
        drawfillbox (brickx5, bricky5, brickx6, bricky6, brick_colour)
        drawfillbox (brickx7, bricky7, brickx8, bricky8, brick_colour)
        Player
        View.Update
        delay (speed)
        cls

        ballx -= xpos
        bally += ypos
        ypos -= gravity

        if bally <= bricky2 + 10 and bally >= bricky + 10 and ballx <= brickx2 and ballx + 10 >= brickx then %%i assume 10 is the size of the ball?
            ypos := 10 - gravity
        elsif bally <= bricky4 + 10 and bally >= bricky3 + 10 and ballx <= brickx4 and ballx + 10 >= brickx3 then
            ypos := 10 - gravity
        elsif bally <= bricky6 + 10 and bally >= bricky5 + 10 and ballx <= brickx6 and ballx + 10 >= brickx5 then
            ypos := 10 - gravity
        elsif bally <= bricky8 + 10 and bally >= bricky7 + 10 and ballx <= brickx8 and ballx + 10 >= brickx7 then
            ypos := 10 - gravity
            fail := true
        end if



        if bally >= 230 then
            score += 5

            doty -= 2

            doty2 -= 2

            doty3 -= 2

            doty4 -= 2

            doty5 -= 2

            doty6 -= 2

            doty7 -= 2

            doty8 -= 2

            bally -= 2

            bricky -= 2

            bricky2 -= 2

            bricky3 -= 2

            bricky4 -= 2

            bricky5 -= 2

            bricky6 -= 2

            bricky7 -= 2

            bricky8 -= 2
        end if

        if bally > maxy then

            score += 5

            doty -= 2

            doty2 -= 2

            doty3 -= 2

            doty4 -= 2

            doty5 -= 2

            doty6 -= 2

            doty7 -= 2

            doty8 -= 2

            bally -= 2

            bricky -= 2

            bricky2 -= 2

            bricky3 -= 2

            bricky4 -= 2

            bricky5 -= 2

            bricky6 -= 2

            bricky7 -= 2

            bricky8 -= 2
        end if

        if doty2 <= 0 then
            doty := maxy
            doty2 := doty + 15
        end if

        if doty4 <= 0 then
            doty3 := maxy
            doty4 := doty3 + 15
        end if

        if doty6 <= 0 then
            doty5 := maxy
            doty6 := doty5 + 15
        end if

        if doty8 <= 0 then
            doty7 := maxy
            doty8 := doty7 + 15
        end if

        if bricky2 <= 0 then
            brickx := Rand.Int (0, 250 - 40)
            bricky := Rand.Int (maxy, maxy + 10)
            brickx2 := brickx + 40
            bricky2 := bricky + 15
            Score := true
        end if

        if bricky4 <= 0 then
            brickx3 := Rand.Int (0, 250 - 40)
            bricky3 := Rand.Int (maxy, maxy + 10)
            brickx4 := brickx3 + 40
            bricky4 := bricky3 + 15
            Score := true
        end if

        if bricky6 <= 0 then
            brickx5 := Rand.Int (0, 250 - 40)
            bricky5 := Rand.Int (maxy, maxy + 10)
            brickx6 := brickx5 + 40
            bricky6 := bricky5 + 15
            Score := true
        end if

        if bricky8 <= 0 then
            brickx7 := Rand.Int (0, 250 - 40)
            bricky7 := Rand.Int (maxy, maxy + 10)
            brickx8 := brickx7 + 40
            bricky8 := bricky7 + 15
            Score := true
        end if

        if bally <= 15 and fail = true then
            die := true

            if save = false then
                best := score
                put : rec, best
                close : rec
                save := true
            end if

            doty += 4

            doty2 += 4

            doty3 += 4

            doty4 += 4

            doty5 += 4

            doty6 += 4

            doty7 += 4

            doty8 += 4

            bricky += 4

            bricky2 += 4

            bricky3 += 4

            bricky4 += 4

            bricky5 += 4

            bricky6 += 4

            bricky7 += 4

            bricky8 += 4
        end if

        if doty2 >= maxy and die = true then
            doty := 0
            doty2 := doty + 15
        end if

        if doty4 >= maxy and die = true then
            doty3 := 0
            doty4 := doty3 + 15
        end if

        if doty6 >= maxy and die = true then
            doty5 := 0
            doty6 := doty5 + 15
        end if

        if doty8 >= maxy and die = true then
            doty7 := 0
            doty8 := doty7 + 15
        end if

        if bricky2 >= maxy and die = true then
            brickx := Rand.Int (0, 250 - 40)
            bricky := 0
            brickx2 := brickx + 40
            bricky2 := bricky + 15
        end if

        if bricky4 >= maxy and die = true then
            brickx3 := Rand.Int (0, 250 - 40)
            bricky3 := 0
            brickx4 := brickx3 + 40
            bricky4 := bricky3 + 15
        end if

        if bricky6 >= maxy and die = true then
            brickx6 := Rand.Int (0, 250 - 40)
            bricky5 := 0
            brickx6 := brickx5 + 40
            bricky6 := bricky5 + 15
        end if

        if bricky8 >= maxy and die = true then
            brickx7 := Rand.Int (0, 250 - 40)
            bricky7 := 0
            brickx8 := brickx7 + 40
            bricky8 := bricky7 + 15
        end if

        if bally <= 15 and die = false then
            ypos := 10 - gravity
        end if

        if ballx > maxx + 10 then
            ballx := -10
        end if

        if ballx < -10 then
            ballx := maxx + 10
        end if

        if xpos >= 3 then
            xpos := 2
        end if
        if xpos <= -3 then
            xpos := -2
        end if

        if score >= destination then
            destination += 1200
            speed -= 1
            level += 1
        end if

        locate (3, 1)
        colour (105)
        put "LVL: ", level

        locate (1, 1)
        colour (105)
        put "Next Level: ", destination

        locate (1, maxcol - 11)
        colour (105)
        put "Score: ", score

        Input.KeyDown (keys)
        if keys (KEY_RIGHT_ARROW) then
            xpos -= 2
        end if
        if keys (KEY_LEFT_ARROW) then
            xpos += 2
        end if
    end loop
end game

procedure instruction     %instruction procedure
    cls
    colour (105)
    put repeat ("%", maxcol)
    locate (2, maxcol div 2 - 6)

    put "Instructions"
    locate (4, 1)

    put "Move Happy Ball with the Left and Right arrows"
    put repeat ("%", maxcol)
    var Play2 : int := GUI.CreateButton (maxx div 2 - 20, maxy div 2, 0, "Play",     %Creating a button called play
        game)
    View.Update     %avoiding the flicker

    loop
        exit when GUI.ProcessEvent     %exiting when pressed
    end loop

end instruction

procedure menu      %the menu procedure
    fontID := Font.New ("Ariel:25:bold")        %The font
    Font.Sizes (fontID, height, ascent, descent, internalLeading)       %font size
    Font.Draw ("Happy Ball", 43, 350, fontID, black)
    drawfilloval (round (ballx), round (bally + 130), 10, 10, Colour)     %body
    drawoval (round (ballx), round (bally + 130), 10, 10, face)     %body outline
    drawfilloval (round (ballx) - 3, round (bally + 130) + 2, 1, 1, face)     %left eye
    drawfilloval (round (ballx) + 3, round (bally + 130) + 2, 1, 1, face)     %right eye
    drawfillarc (round (ballx), round (bally + 130) - 2, 2, 4, 180, 360, face)     %smile
    drawfillbox (brickx, bricky, brickx2, bricky2, brick_colour)

    var Instruc : int := GUI.CreateButton (maxx div 2, maxy div 2, 0, "Instructions",     %Creating a button called instructions
        instruction)
    View.Update
    var Play : int := GUI.CreateButton (maxx div 2 - 70, maxy div 2, 0, "Play",     %Creating a button called play
        game)
    View.Update
    loop
        exit when GUI.ProcessEvent     %exiting when pressed
    end loop

end menu

menu
Chaoskiller




PostPosted: Sun Oct 25, 2009 9:09 pm   Post subject: RE:[Tutorial] File I/O

nice. I wish i could do that but right now im currently working on a pacman game.. It wasnt going well until i saw that you made the happy face a whole instead of little parts which makes it easier cause i can tell it to run that procedure whenever Smile
ScaryRat




PostPosted: Sun Apr 18, 2010 9:05 pm   Post subject: RE:[Tutorial] File I/O

You should add "assert" to check for validity.
Velocity




PostPosted: Mon Nov 28, 2011 8:43 am   Post subject: RE:[Tutorial] File I/O

I used the example for a flexible array and i want to know how to output a random string from the text file to my turing screen, just to test it. Cause im making hangman, and i already have the word list created, i did the exact thing for the word list.
My question is, how do i output a random word from the word list to the screen?
Insectoid




PostPosted: Mon Nov 28, 2011 9:42 am   Post subject: RE:[Tutorial] File I/O

Pick a random number between one and the size of your array. Output the word at that spot.

var foo : int
foo = Rand.Int (1, 10)
put array_of_words(foo)
Velocity




PostPosted: Mon Nov 28, 2011 10:16 am   Post subject: RE:[Tutorial] File I/O

is array_of_words the array name?
evildaddy911




PostPosted: Fri Dec 09, 2011 6:32 pm   Post subject: Re: [Tutorial] File I/O

Quote:
get : file , *

this looks like string manipulation... if I needed a specific spot in a file, could i use
Turing:
get : [file], [variable] : [byte number]

to get a specific character and store it as an int/string?
because Im making a game where you may possibly create your own levels stored as text files
Display posts from previous:   
   Index -> Programming, Turing -> Turing Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 2 of 3  [ 31 Posts ]
Goto page Previous  1, 2, 3  Next
Jump to:   


Style:  
Search: