locked
How to Format and Double-Space Text Written to a File? RRS feed

  • Question

  • Hello, everyone.

    May I please request a little bit of help with a Small Basic program I'm writing?  I simply want to format the output file a certain way.

    I want the user to input text into six different text windows.  Then, I want the program to open up a text file, and store the inputted text. 

    Here is the code I have written so far, which as of now has only two text windows:

    TextWindow.write("Please enter the Title:  ")

    title = TextWindow.read()

    TextWindow.write("Please enter the Director:  ")

    director = TextWindow.read()

    File.WriteContents("c:\smallbasic.txt", "Title:  " + title + "Director:  " + director)

    The last line of code is actually one entire line.  I mention this for a reason, which I'll get to in a moment.

    Here is the output of the program:

    Title:  Annie HallDirector:  Woody Allen

    I have two questions:

    1.  How do I create a new line, so that Title:  Annie Hall  and Director:  Woody Allen are on separate lines?  Is there a command specifically for something like this?

    2.  Eventually, the last line will be rather lengthy, and will be confusing to work with.  Is there a way to break up this line into several lines, without it screwing up the programming?   I know that in Visual Basic 2010, the underscore symbol ( _ ) lets you do this         .  What is the Small Basic equivalent?  Is it possible for me to request an example?

    Thank you very much for any assistance you care to give.

    J. Danniel (Returning to the world of programming at age 51 after a 30-year absence….)

     

    Thursday, June 2, 2011 9:33 PM

Answers

  • Hi,

    1] We can output on separate lines using File.WriteLine, for example:

    TextWindow.write("Please enter the Title:  ")
    title = TextWindow.read()
    TextWindow.write("Please enter the Director:  ")
    director = TextWindow.read()

    line = 1
    File.WriteLine("c:\temp\smallbasic.txt",line,"Title:  " + title)
    line = 2
    File.WriteLine("c:\temp\smallbasic.txt",line,"Director:  " + director)

    This sort of covers point 2 since each line is done individually and the lines are shorter - there is no continuation character in SB - just very long lines if you can't do it in shorter lines - strings can always be formed in stages.

    output = "Title:  " + title
    output = output + "Director:  " + director
    File.WriteContents("c:\smallbasic.txt", output)

    We are repeating similar code using the WriteLine and we could use loops and arrays for this - if you fancy this approach then have a go and post again if you have questions.  Here is one way title and directors could be read into arrays, then later processed into a file.

    i = 1
    While (i > 0)
      TextWindow.write("Please enter the Title:  ")
      title[i] = TextWindow.read()
      TextWindow.write("Please enter the Director:  ")
      director[i] = TextWindow.read()
      TextWindow.write("Another film (y)")
      If (Text.ConvertToLowerCase(TextWindow.read()) = "y") Then
        i = i+1
      Else
        i = 0
      EndIf
    EndWhile



    • Marked as answer by litdev Tuesday, June 21, 2011 8:32 PM
    Thursday, June 2, 2011 10:14 PM
  • In the Data Extension found at http://social.msdn.microsoft.com/Forums/en-US/smallbasic/thread/d001e5ab-5180-49c6-9894-8d16f6a65e0b, you can use this code:

    'this starts Notepad, and tells it to open a file found at "C:\Test.txt". You can

    'replace this with another filepath too. this does not need to be a *.txt file.

    Process.StartWithArguments("C:\Windows\System32\notepad.exe", "C:\Test.txt")

     

    You can also replace C:\Test.txt with another filepath. This just starts notepad and feeds it an argument, telling it to open the requested file.

    To install the extension:

    1. Download the .exe

    2. Run the .exe

    3. Close (if open) Small Basic, and reopen it.

    4. Copy+Paste the code in italics above.


    ~~AirWaves!!~~
    • Marked as answer by litdev Tuesday, June 21, 2011 8:32 PM
    Friday, June 3, 2011 5:59 AM

All replies

  • Hi,

    1] We can output on separate lines using File.WriteLine, for example:

    TextWindow.write("Please enter the Title:  ")
    title = TextWindow.read()
    TextWindow.write("Please enter the Director:  ")
    director = TextWindow.read()

    line = 1
    File.WriteLine("c:\temp\smallbasic.txt",line,"Title:  " + title)
    line = 2
    File.WriteLine("c:\temp\smallbasic.txt",line,"Director:  " + director)

    This sort of covers point 2 since each line is done individually and the lines are shorter - there is no continuation character in SB - just very long lines if you can't do it in shorter lines - strings can always be formed in stages.

    output = "Title:  " + title
    output = output + "Director:  " + director
    File.WriteContents("c:\smallbasic.txt", output)

    We are repeating similar code using the WriteLine and we could use loops and arrays for this - if you fancy this approach then have a go and post again if you have questions.  Here is one way title and directors could be read into arrays, then later processed into a file.

    i = 1
    While (i > 0)
      TextWindow.write("Please enter the Title:  ")
      title[i] = TextWindow.read()
      TextWindow.write("Please enter the Director:  ")
      director[i] = TextWindow.read()
      TextWindow.write("Another film (y)")
      If (Text.ConvertToLowerCase(TextWindow.read()) = "y") Then
        i = i+1
      Else
        i = 0
      EndIf
    EndWhile



    • Marked as answer by litdev Tuesday, June 21, 2011 8:32 PM
    Thursday, June 2, 2011 10:14 PM
  • Thank you very much for replying and for assisting.  The code you supplied works.  I'm not ready yet to work with the Do-While/If-Else code, because I haven't progressed to the point of understanding those commands yet.  I'm sure I will soon.

    I have a follow-up question, if I may, please:

    The code you supplied does create a separate line for each item.  However, after seeing the output, I realized I need the text file to be double-spaced.  There needs to be a blank line between each line.  Example:

    Instead of:

    Title: Annie Hall
    Director:  Woody Allen

    I need it to look like this:

    Title:  Annie Hall

    Director:  Woody Allen

    I know how to do this with Visual Basic 6 and Visual Basic 2010 Express, but not Small Basic.  What's the best way to add a blank line?

    Oh...can I please ask just one more question?  If I want the text file to be displayed after the program runs, what command should I use?

    In Visual Basic, the code would be:

    Process.Start("C:\smallbasic.txt")

    What is the Small Basic equivalent?

     

    Those should probably be all the questions I have for this project.  Thank you again for assisting.

    Jd

     

    Thursday, June 2, 2011 11:07 PM
  • TextWindow.WriteLine("")?
    ~~AirWaves!!~~
    Friday, June 3, 2011 5:53 AM
  • In the Data Extension found at http://social.msdn.microsoft.com/Forums/en-US/smallbasic/thread/d001e5ab-5180-49c6-9894-8d16f6a65e0b, you can use this code:

    'this starts Notepad, and tells it to open a file found at "C:\Test.txt". You can

    'replace this with another filepath too. this does not need to be a *.txt file.

    Process.StartWithArguments("C:\Windows\System32\notepad.exe", "C:\Test.txt")

     

    You can also replace C:\Test.txt with another filepath. This just starts notepad and feeds it an argument, telling it to open the requested file.

    To install the extension:

    1. Download the .exe

    2. Run the .exe

    3. Close (if open) Small Basic, and reopen it.

    4. Copy+Paste the code in italics above.


    ~~AirWaves!!~~
    • Marked as answer by litdev Tuesday, June 21, 2011 8:32 PM
    Friday, June 3, 2011 5:59 AM