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

myWriter.WriteLine(item)

Next

myWriter.Close()

Console.WriteLine("Writing Done")

Console.ReadLine()

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

Relevance
  • 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)

    Loop

    FileClose(fileNumber)

    ' writing to the file

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

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

    FileClose(fileNumber)

    Console.ReadLine()

    End Sub

    End Module

    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

    Kill = delete files

    http://msdn.microsoft.com/en-us/library/ey1w10ch%2...

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

    http://msdn.microsoft.com/en-us/library/c3zyf7d8%2...

    EOF = End Of File

    http://msdn.microsoft.com/en-us/library/7ct2yy4s%2...

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

    http://msdn.microsoft.com/en-us/library/tty8298b%2...

    FileOpen = Opens a file for input or output.

    http://msdn.microsoft.com/en-GB/library/afh37kh8%2...

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

    http://msdn.microsoft.com/en-GB/library/0s9sa1ab%2...

    hope that helps

Still have questions? Get your answers by asking now.