none
Small Basic Technet tutorial - lesson 3.1 RRS feed

  • Question

  • I'm trying to work through this tutorial at the moment and I'm stuck, I have no idea whether the file is meant to be in temp already or how to get it there etc. 

    here is the code that's meant to be a solution:

    FilePath = "C:\temp\TempSubdirectory\my.txt"

    TextWindow.WriteLine("Write Content = " + File.WriteLine(FilePath, 1, "Shakespeare was a great writer."))

    TextWindow.WriteLine("Append Content = " + File.AppendContents(FilePath, "He wrote many plays."))

    TextWindow.WriteLine("Read Content = " + File.ReadContents(FilePath))

    however it just returns the error messages -

    Write content - FAILED

    Append content - FAILED

    I'm not sure what to do, or how to create this file :/

    Monday, November 25, 2013 10:16 PM

Answers

  • My guess is that you need to create the director C:\temp first.  If you don't have access to the C drive to create this directory, then save your SmallBasic file somewhere and this can be your file location if you use:

    FilePath = Program.Directory+"\my.txt"Program.Directory will be the folder you saved your program to.

    Monday, November 25, 2013 10:22 PM
    Moderator
  • Rather than arbitrarily specify a temporary folder on your own, ask Small Basic to find 1 for ya!  :D

    You get a file w/ extension ".tmp":

    tempPath = File.GetTemporaryFilePath()

    Most useful though is to get the folder where a Small Basic program is running from.

    From there, you get a fixed place to save and load resources:

    folderPath = Program.Directory

    Another interesting option is a file containing custom settings. It generates a file w/ extension ".settings":

    settingsPath = File.GetSettingsFilePath()


    Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

    Tuesday, November 26, 2013 12:48 AM
    Answerer

All replies

  • My guess is that you need to create the director C:\temp first.  If you don't have access to the C drive to create this directory, then save your SmallBasic file somewhere and this can be your file location if you use:

    FilePath = Program.Directory+"\my.txt"Program.Directory will be the folder you saved your program to.

    Monday, November 25, 2013 10:22 PM
    Moderator
  • cheers Lit - I have access to the C:\ drive. 

    Yeh, If i change the URL from the one given to the URL of a blank notepad instance on the desktop it works (writes stuff about shakespeare in there) But the temp file stuff was kind of confusing... 

    Maybe the temp file isn't really the point, and I've missed it, again... !

    cheers

    Monday, November 25, 2013 10:35 PM
  • Rather than arbitrarily specify a temporary folder on your own, ask Small Basic to find 1 for ya!  :D

    You get a file w/ extension ".tmp":

    tempPath = File.GetTemporaryFilePath()

    Most useful though is to get the folder where a Small Basic program is running from.

    From there, you get a fixed place to save and load resources:

    folderPath = Program.Directory

    Another interesting option is a file containing custom settings. It generates a file w/ extension ".settings":

    settingsPath = File.GetSettingsFilePath()


    Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

    Tuesday, November 26, 2013 12:48 AM
    Answerer
  • I just tried this with settingsPath = File.GetSettingsFilePath()

    The contents returned Success but there are no contents.

    Copy returned Failed.

    The tempFile worked but it's empty, probably because I didn't write anything to it.

    What is the Settings File and how can it be used and why does it seem empty?

    'tempPath = File.GetTemporaryFilePath()
    settingsPath = File.GetSettingsFilePath()
    
    TextWindow.WriteLine(settingsPath)
    
    contents = File.ReadContents(settingsPath)
    
    'returned Success
    TextWindow.WriteLine(File.WriteContents(Program.Directory + "\temp.txt", contents))
    
    'returned Failed
    TextWindow.WriteLine(File.CopyFile(settingsPath, Program.Directory + "\temp.txt"))  
    
    'settingsPath returns ProgramFileName.settings
    'intellisense says it should be "c:\temp\settings.data"

    Tuesday, November 26, 2013 8:31 AM
    Moderator
  • Cheers Loop - the GetTemporyPath worked well :)

    The Program.Directory show's up "C:\User\username\AppData\Local\Temp" for me. 

    The File.GetSettingsFilePath() shows something within the same location ("AppData\Local\Temp") 

    I'm guessing that they are alright? 

    Thanks though

    Tuesday, November 26, 2013 12:37 PM
  • Neither File.GetTemporaryFilePath() nor File.GetSettingsFilePath() generates content!

    They merely returns a full path to a file!

    After that, we can do anything to any of them (read, write, append, delete, etc)!


    Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

    Tuesday, November 26, 2013 1:24 PM
    Answerer
  • Cool, thanks :)
    Tuesday, November 26, 2013 1:26 PM
  • I've got a game in which I've used File.GetSettingsFilePath() w/ File.WriteContents() + File.ReadContents() to record game configurations.  :D

    death-valley-how-to-overcome-sbs-lack-of-inkey-function-for-textwindow-console-mode

    Code Import: KTX387-1:


    Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

    Tuesday, November 26, 2013 1:32 PM
    Answerer
  • I crashed into the ravine wall :(

    good stuff though, hopefully I'll be able to get my head around that soon :)


    • Edited by TeaWatch Tuesday, November 26, 2013 1:40 PM
    Tuesday, November 26, 2013 1:39 PM
  • Teawatch & gotoloop

    Hi TeaWatch,   re goto loops sample Game.

    "you crashed into the wall".... yeah me too. Until I worked out that to steer you use "Q" & "W" keys.

    Hi goto:  I instinctively went to use left/right arrow keys to steer and kept crashing, and this put me off the game quite a bit until I worked q/w. Then the game played perfectly and was entertaining.

    I wish you'd put the game play instructions in at the start.

    This is a classic and very useful code sample. I'd like to nominate it for the blog, but I reckon it needs the instructions first. This is from the users point of view.


    btw: I got sorted on the getfileSettings. thx goto, thx to that sample. I just got to work out GetArguments part now. Very interesting sample
    Tuesday, November 26, 2013 8:58 PM
    Moderator
  • Oh! I haven't realized most people weren't figuring out key configuration!
    It's just 'Q' or 'A' to left-steer and any other for right-steer!

    I thought b/c game is released under source form it'd be common sense
    to look for ourselves where code checks for keys!

    I know most folks would try to use arrow keys 1st,
    but I dunno how to check for them under TextWindow mode!  =(

    '  TextWindow ASCII Code:
    
    Loop:
    
    Key = TextWindow.ReadKey()
    
    TextWindow.Write(Key)
    TextWindow.Write("  :  ")
    TextWindow.WriteLine(Text.GetCharacterCode(Key))
    
    Goto Loop

    Actually, if I hadn't realized to use separate threads for key check & animation,

    an action-type game wouldn't be possible w/o GraphicsWindow!
    We can see that even the hidden TextWindow.ReadKey() halts the program!

    Anyways, I've included extra instructions for it and new code import is -> KTX387-2.


    Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

    Wednesday, November 27, 2013 10:48 AM
    Answerer
  • Thanks goto. Thought I should let you know.

    Sometimes when I run a program and it doesn't do what I expect it to I don't look any further. I'm glad I did in this case.

    And this is classic bit of code. Very helpful. I'm still working out how some of it works.

    I like trying out the parallel processing using the Timer. Like running stuff in the background, maybe good for downloading stuff, doing calcs etc.

    The Program.Arguments has just got me interested in writing batch files. Happy here.

    Wednesday, November 27, 2013 12:45 PM
    Moderator
  • Since I've converted this console-type game from an old computer book to demonstrate the usage of threads to overcome the lack of an InKey() analogous function for the TextWindow, I've decided to include more rare seen stuff like File.GetSettingsFilePath() & Program.GetArgument() too.  :D

    Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

    Wednesday, November 27, 2013 2:51 PM
    Answerer
  • Hey goto

    Is it ok to nominate this ( KTX387-2) for blog feature?

    Wednesday, November 27, 2013 7:03 PM
    Moderator
  • Go ahead! Even modify it if you wish to!  :D

    Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

    Wednesday, November 27, 2013 9:33 PM
    Answerer