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

Username:   Password: 
 RegisterRegister   
 Saving and Loading files.
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
TheLastFall




PostPosted: Tue Oct 17, 2006 8:34 am   Post subject: Saving and Loading files.

Ok, I want to have a Score list but I don't know how to import a file and save the scores. I'd also like to know how to save initials of the person to the scores list also but I pretty sure I can do that if I can just figure out how to save the scores. Can anyone please help me? It would be much appreciated. Is it anything like:
code:
import: 'scores'

I actually have no clue how it would look, that is kind of a guess.[/code]
Sponsor
Sponsor
Sponsor
sponsor
Dan




PostPosted: Tue Oct 17, 2006 11:08 am   Post subject: (No subject)

We have serval tutorals on using files in turing. Here are a few:


File I/O: http://www.compsci.ca/v2/viewtopic.php?t=12972

High Score List: http://www.compsci.ca/v2/viewtopic.php?t=5340

Learn how to use File IO - easy!: http://www.compsci.ca/v2/viewtopic.php?t=6818


I sugest using the sreach fuction of the site if you need help on a topic that could have a tutoral for it. Also the import fuction is for importing code not noraml files.
Computer Science Canada Help with programming in C, C++, Java, PHP, Ruby, Turing, VB and more!
TheLastFall




PostPosted: Wed Oct 18, 2006 7:33 am   Post subject: (No subject)

Yea, about that. I am what a lot of people call, even myself an idiot. I do not understand what I'm doing with the 'Hi Scores' link, or the other two. All I want to do is just save a score with the persons name, like so:
code:
Score:      Name:
     
         1000        Ass

That is all. It doesn't seem hard at all but I just don't understand how to save to the file and load it at the end of the program. So everyone could see the past scores that were made. A pretty simple procedure for those who have done importing and exporting in the past. It's just I kind of need things described to me specifically. I'm a moron so please can you make it clear.[/code]
TheLastFall




PostPosted: Wed Oct 18, 2006 7:36 am   Post subject: (No subject)

I also forgot to mention, there are tons of spelling mistakes in the 'Hi Score' link. I don't know about other people but I found that really distracting.
Tony




PostPosted: Wed Oct 18, 2006 8:03 am   Post subject: (No subject)

alright, lets take it one step at a time. Are you able to follow the File I/O tutorial and read/write _any_ text from/to a file? Doesn't have to be highscore, just a name.

If not, what part are you having problems understanding?
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
TheLastFall




PostPosted: Wed Oct 18, 2006 11:23 am   Post subject: (No subject)

I can't say I do, it's really confusing.
TheLastFall




PostPosted: Wed Oct 18, 2006 11:25 am   Post subject: (No subject)

Well I understand the loading the text file part but then the rest just kind of doesn't make any sense.
Tony




PostPosted: Wed Oct 18, 2006 11:58 am   Post subject: (No subject)

ok, so you've loaded a file as stream

now it's the same as console based put/get
code:

put : stream, "write to file"
get : stream, readVariable


does that make sense so far?
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Sponsor
Sponsor
Sponsor
sponsor
TheLastFall




PostPosted: Wed Oct 18, 2006 5:07 pm   Post subject: (No subject)

Yes, that makes a lot of sense. Also my score interger is tm is it any problem because it's an interger?
TheLastFall




PostPosted: Thu Oct 19, 2006 11:46 am   Post subject: (No subject)

Ok, I have to score being saved to txt. Just theres a little problem, ever time I enter my intials and new score it replaces the last one. Is there anything I can do? Also, I want it to say Enter Your Initials: and have it visible at a certain point and a certain size. This is what I have
code:

drawfillbox (0, 0, maxx, maxy, 255)
Font.Draw ("Game Over", maxx div 2 - 400, maxy div 2 - 25, font, 119)
Font.Draw ("Enter Your Initials ", maxx div 2 - 200, maxy div 2 - 75, font1, 122)
colorback (255)
color (122)
get name
put : stream, name, "    ", tm

I would like to know how to make it so they can't type past three characters. I can get the name with no problem, but I can't see what I typed because its black and the size of what I typed is about the 12 sized font on MSoft Word. I there any way to have it so that the entering intials will be the same size as the Enter Your Initials font and have it produced right after the text? I know there is a simple way but I'm just blinded right now.
Basically my txt file looks like this:

Ass 283

But I want it more like:

Initials: Score:
Ass 283
Rox 592

and so on.
I want the intial part to look like:

Game Over
Enter Your Initials {Ass}

I can easily figure out how to load up the screen of the scores.
neufelni




PostPosted: Thu Oct 19, 2006 4:15 pm   Post subject: (No subject)

TheLastFall wrote:
Ok, I have to score being saved to txt. Just theres a little problem, ever time I enter my intials and new score it replaces the last one. Is there anything I can do?

You need to use seek in order to add the new scores to the end of the file. Just add the word seek to your open file statement. Like this:
code:
open : stream, "highscores.txt", get, put, seek

And then when you want to put your highscores to the end of the file, add this line before you you use the put statement to put the scores in the file:
code:
seek : stream, *

The seek command makes it so that the next output will begin at the specified position in the file. The * makes it so that position is at the end of the file.
TheLastFall wrote:

Also, I want it to say Enter Your Initials: and have it visible at a certain point and a certain size. This is what I have
code:

drawfillbox (0, 0, maxx, maxy, 255)
Font.Draw ("Game Over", maxx div 2 - 400, maxy div 2 - 25, font, 119)
Font.Draw ("Enter Your Initials ", maxx div 2 - 200, maxy div 2 - 75, font1, 122)
colorback (255)
color (122)
get name
put : stream, name, "    ", tm

I would like to know how to make it so they can't type past three characters. I can get the name with no problem, but I can't see what I typed because its black and the size of what I typed is about the 12 sized font on MSoft Word. I there any way to have it so that the entering intials will be the same size as the Enter Your Initials font and have it produced right after the text? I know there is a simple way but I'm just blinded right now.

I explained how to get input from the user with Font.Draw in another thread. Here it is:
Nick wrote:

If you want to get input using Font.Draw, it is a bit more complicated. You have to get one letter at a time using a loop.

code:

View.Set ("noecho, nocursor") % this makes it so that you can't see the user's input in the normal font.

var letter : string (1)
var fullword : string := ""
var font : int := Font.New ("Arial:12")

loop
    getch (letter)
    cls
    exit when letter = (KEY_ENTER)
    if letter = (KEY_BACKSPACE) and length (fullword) > 0 then
        fullword := fullword (1 .. length (fullword) - 1)
    else
        fullword := fullword + letter
    end if
    Font.Draw (fullword, 10, 10, font, 7)
end loop

And to get it so that the user can only enter three characters, just add an if statement to my program above so that length(initials) has to be <= 3.
TheLastFall




PostPosted: Thu Oct 19, 2006 4:35 pm   Post subject: (No subject)

That will help a lot, thank you.
TheLastFall




PostPosted: Fri Oct 20, 2006 8:26 am   Post subject: (No subject)

Ok, it doesn't really work all that good. First of all, the
code:
open : stream, "score.txt", get, put, seek
seek : stream, *
put : stream, fullword, " ------ ", tm

is not working, it just ends up replacing the text that was set before.

The Font.Draw isn't working properly.

code:
var letter : string (1)
var fullword : string := ""
for i : 1 .. 3
    getch (letter)
    exit when letter = (KEY_ENTER)
    if letter = (KEY_BACKSPACE) and length (fullword) > 0 then
        fullword := fullword (1 .. length (fullword) - 1)
    else
        fullword := fullword + letter
    end if
    Font.Draw (fullword, maxx div 2 + 75, maxy div 2 - 125, font4, 117)
end for

That is what I have. I want the name/initials to be visable while being inputed while the Enter your initials is also on the screen. It just won't appear. I'd have to enter my intials and then the Enter your initials would appear.
neufelni




PostPosted: Fri Oct 20, 2006 10:07 am   Post subject: (No subject)

Sorry about that. I figured out what's wrong. All you have to do is add the word mod to the end of your open stream statement, along with put, get, and seek.

And for the Font.Draw, you don't want to make the loop a for loop from 1 - 3. If you do this them the user won't be able to backspace and change it if he makes a mistake. All that you want to do is add another if statement in the else branch of the already existing if statement. Here is the code, and this time I believe everything is working fine:
code:
View.Set ("noecho, nocursor") % this makes it so that you can't see the user's input in the normal font.

var stream : int
var letter : string (1)
var fullword : string := ""
var font : int := Font.New ("Arial:12")
var tm : int := 3

open : stream, "score.txt", get, put, seek, mod

Font.Draw ("Enter your Initials: ", 10, 10, font, 7)

loop
    getch (letter)
    cls
    exit when letter = (KEY_ENTER)
    if letter = (KEY_BACKSPACE) and length (fullword) > 0 then
        fullword := fullword (1 .. length (fullword) - 1)
    else
        if length (fullword) < 3 then
            fullword := fullword + letter
        end if
    end if
    Font.Draw ("Enter your Initials: ", 10, 10, font, 7)
    Font.Draw (fullword, 140, 10, font, 7)
end loop

seek : stream, *
put : stream, fullword, " ------ ", tm

Freakman just posted a function for getting input from the user with Font.Draw. You should check it out, as it is better than mine.
http://www.compsci.ca/v2/viewtopic.php?t=13777
Clayton




PostPosted: Fri Oct 20, 2006 12:53 pm   Post subject: (No subject)

Did you really read all of those tutorials thouroghly? All of the questions you have asked so far have been in there (except for the mod question). Read more carefully next time Wink

Another thing you can do to get and display in font is use the function i published to do that (its a better idea to compartmentalize your code) and is done almost exactly the same way as your method above.
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  [ 30 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: