How To Read And Write Files
Print Hello World
Write Basic Syntax
Use Properties
Make An If Else Ladder
Make A Branch Statement
Declare Inits
Use Lists
Use Arrays
Make A Class Hierarchy
Use Nil And Nilable Types
Use Dynamic Typing
Declare Variable Number Of Args
Read And Write Files
Check Inheritance And Implementation
Customize Object Equality
Pass References To Methods
Translate Pseudo Code To Cobra 1
Translate Pseudo Code To Cobra 2
Implement IEnumerable 1
Implement IEnumerable 2
Iterate Through Recursive Data With Yield
Make A Collection Class
Declare Contracts
Threads
Win Forms
WPF
GTK
Qyoto
Access MySQL
XNA
Open TK
 
"""
Reading files in Cobra relies on the libraries and therefore is pretty the much
the same as C# or VB.

See the BCL (Base Class Library) docs for File, StreamReader, StreamWriter,
TextReader and TextWriter for more.
"""


class Test

    def main
        nl = CobraCore.newLine
        fileName = 'test-file.text'

        # write a whole file
        File.writeAllText(fileName, 'This is a test file.[nl]To make sure there is a file to work with.[nl]')

        # read a whole file
        contents = File.readAllText(fileName)
        assert 'test' in contents   # The `in` operator works for strings: <substring> in <string> ==> bool

        # read line by line
        num = 1
        using sr = File.openText(fileName)
            while true
                line = sr.readLine
                if line is nil, break
                print '[num]. len=[line.length] "[line.trim]"'
                num += 1

        # be nice about IO errors
        fileName = 'bad-file-name.text'
        try
            contents = File.readAllText(fileName)
        catch ioe as IOException
            print 'I/O Error: [ioe.message]'
        success
            assert 'test' in contents