Help! Reading text files

I have to write a program to make a 10 question multiple choice quiz.

The 'teacher' needs to be able to write questions which HAVE to be saved to a text file (done that bit).

The text file then needs to be read to create the quiz for the 'pupil' (can't get it to do that bit).

Can anyone help?

Comments

  • [b][red]This message was edited by DrMarten at 2006-5-29 13:15:32[/red][/b][hr]

    : I have to write a program to make a 10 question multiple choice quiz.
    :
    : The 'teacher' needs to be able to write questions which HAVE to be saved to a text file (done that bit).
    :
    : The text file then needs to be read to create the quiz for the 'pupil' (can't get it to do that bit).
    :
    : Can anyone help?

    ======================================================================
    Hi,

    Put these lines at the very TOP of your code window:>>
    [code]
    OPTION STRICT OFF
    Imports System
    Imports System.IO
    Imports System.Text
    [/code]

    Then:>>

    Here is a code snippet.
    [code]
    Try
    If File.Exists(path) Then
    File.Delete(path)
    End If

    Dim sw As StreamWriter = New StreamWriter(path)
    sw.WriteLine("This")
    sw.WriteLine("is some text")
    sw.WriteLine("to test")
    sw.WriteLine("Reading")
    sw.Close()

    Dim sr As StreamReader = New StreamReader(path)

    Do While sr.Peek() >= 0
    textbox1.Appendtext((Convert.ToChar(sr.Read())))
    Loop
    sr.Close()
    Catch e As Exception
    textbox1.Appendtext("The process failed: {0}", e.ToString())
    End Try
    [/code]

    [b][blue]path[/blue][/b] would be just "myfile.txt" or similiar if the file is in the BIN folder of your project, including the quotes,
    otherwise something like>>

    "c:myfile.txt"

    E.G.

    [code]
    Dim sr As StreamReader = New StreamReader("c:myfile.txt")
    'or if the file is in your BIN folder.
    Dim sr As StreamReader = New StreamReader("myfile.txt")
    [/code]


    Dont't forget to close your file(s). :-)

    sw.close()
    'or
    sr.close()


    I think you can write the line[b]
    Dim sr As StreamReader = New StreamReader("c:myfile.txt")[/b]

    as>>
    [b][blue]Dim sr As New StreamReader("c:myfile.txt")
    [/blue][/b]

    instead. Neater and shorter. :-)

    Please note i have used textbox1
    You could of course use ReadLine to get a line at a time from your file, and CSTR instead of Convert.ToChar

    If you read a line at a time you may want to split the line.

    eg.
    [code]

    Dim string1, string2, testString As String
    testString = "Fred Bloggs is not my name!"
    string1 = testString.Split(" ")(0)
    string2 = testString.Split(" ")(1)
    'string1 contains "Fred"
    'string2 contains "Bloggs"
    [/code]

    Note the index starts at zero as string splitting is an array
    based command.


    Hope this helps?




    Regards,

    Dr M.

  • Hm, You said that the lines had to be read to different fields? I'm not too keen in splitting a text file and distributing lines but I think that's your goal, right?
    Mess with the Best, Die like the Rest!
    ----------
    http://www.graalonline.com
    Get started today!

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories