Reading from and Writing to Sequential Files(Visual Basic) Please Help!!!!?

The programming language I'm using is Visual Basic.NET

Can someone explain me how can I read from and write stuff into a sequential file filled with records, like for example: (name and age lol)

"Peter" 20

"Jane" 25

"John" 30

How do I add another record called

"Naruto" 20

to this?

Any ideas?

I'm using Visual Basic Studio Express 2010 (since that's installed on my desktop. My laptop's got 2012 though)

For now

All I know is how to write and read from a single file:(This program demonstrates ALL that I know so far about reading and writing to files)(Written in Console Mode)

Module Module1

'Writing Files

Sub Main()

'Set File Path and create StreamWriter object for Writing files

Dim filePath As String = "E:\myFile.txt"

Dim myWriter As IO.StreamWriter

myWriter = IO.File.CreateText("E:\WritingStuff_Console.txt")

Dim myArray() As String = {"A", "B", "C"}

For Each item As String In myArray




Console.WriteLine("Writing Done")


End Sub

End Module

Also,can someone explain to me what Input,Put,EOF,Freefile,Kill,Open is and what it does(with examples and codes for noobs) ( A full fledged VB program in Console Mode would be totally helpful)

1 Answer

  • 7 years ago
    Favorite Answer

    try this code


    Module Module1

    Sub Main()

    Dim _string As String = "" ' for the strings in the file

    Dim _integer As Integer = 0 ' for the integers in the file

    Dim fileNumber As Integer ' for the file number to handle input/output

    ' here we are using the FreeFile function to supply a file number that is not already in use.

    fileNumber = FreeFile()

    ' reading from the file

    FileOpen(fileNumber, "C:\temp\WritingStuff.txt", OpenMode.Input)

    ' read from the file while not end of file

    Do While Not EOF(fileNumber) ' EOF is End Of File

    Input(fileNumber, _string) ' get each string (ie "Peter")

    Input(fileNumber, _integer) ' get each integer (ie 20)

    ' print both the string and integer to the console

    Console.WriteLine(_string & " " & _integer)



    ' writing to the file

    FileOpen(fileNumber, "C:\temp\WritingStuff.txt", OpenMode.Append)

    WriteLine(fileNumber, "Naruto", SPC(1), 20) ' your new line



    End Sub

    End Module


    Kill = delete files

    Freefile = File number (ID) to handle input/output

    EOF = End Of File

    Input = Reads data from an open sequential file and assigns the data to variables.

    FileOpen = Opens a file for input or output.

    FilePut = Writes data from a variable to a disk file.

    hope that helps

Still have questions? Get your answers by asking now.