Pavol Namer Pavol Namer - 2 years ago 90 Question

Reading and writing lines to and from a file

I am writing a simple console application using Microsoft Visual Basic 2010 express. I am trying to make a "newfile1.txt" file in which will be write something heading and than lets say 10 rows with three words in each row.

After that, I would like to read from file, and write to the "newfile2" file only the second word from file "newfile1.txt"

Than I would like to read from this file every line, and store lets and store only the second word from newfile1.txt

I try to use following code but I don't know how to specify several things (see bellow code)

Module Module1

Sub Main()

Dim i As Integer
FileOpen(1, "C:\Users\Namba\Documents\ANALYZA MD\newFile1.txt", OpenMode.Append, OpenAccess.ReadWrite, OpenShare.Default)
FileOpen(2, "C:\Users\Namba\Documents\ANALYZA MD\newFile2.txt", OpenMode.Append, OpenAccess.ReadWrite, OpenShare.Default)
WriteLine(1, "Heading of the file1")

For i = 1 To 10 Step 1
WriteLine(1, "Word 1" & "Word 2" & "Word 3")

WriteLine(2, "Heading of the file2")
Dim filereader As System.IO.StreamReader
filereader = My.Computer.FileSystem.OpenTextFileReader("C:\Users\Namba\Documents\ANALYZA MD\newFile1.txt")
Dim stringReader As String

For i = 1 To 10 Step 1
stringReader = filereader.ReadLine()
WriteLine(2, stringReader)

End Sub
End Module

So I have several questions:

  1. Is it possible via ReadLine store words lets say in to the array or each word to the different string variable?

  2. Is there simpler form how to open file and read each word, eventual define that the first word will be store in to the string var1, the second in to the var2 and so on, and similar if we have a file with numbers so that I would like to read from this file and store each number in to the some variable.

I can do this in fortran easy, via READ() WRITE() in very simple way

OPEN(UNIT=11, FILE="newfile1.txt)
READ(UNIT=11) x, y, z
OPEN(UNIT=12, FILE="newfile2.txt)
WRITE(UNIT=12,*) y

So this will read from one file the first 3 word(or number if the x, y, z is declare as number) from the first line and write in to the second file just second word (or number).

So I wonder if there is something very similar also in visual basic?

Answer Source

In general, if your files aren't large then it is faster (with respect to code writing) and easier to just read the contents of the file into memory and then manipulate it as needed.

Hopefully these examples will be of some help.

' Read entire contents of file1.txt into an array.
Dim file1 As String() = System.IO.File.ReadAllLines("C:\file1.txt")

' Now extract the 2nd word from each line (assuming all lines have at least 2 words).
Dim secondWords As New List(Of String)
For Each line In file1
    ' Break apart the string by spaces and take the second index (word).
    secondWords.Add(line.Split(" ")(1))

' Write the contents to a new file.
' This new file will have 1 word per line.
System.IO.File.WriteAllLines("C:\file2.txt", secondWords.ToArray())

If you are looking to examine each word, the code can become something like this:

' Read entire contents of file1.txt into an array.
Dim file1 As String() = System.IO.File.ReadAllLines("C:\file1.txt")

' Process each line.
For Each line In file1
    ' Process each word within the line.
    For Each word In line.Split(" ")
        ' Do something with the word.

    ' Or process by word index.
    Dim words As String() = line.Split(" ")
    For i As Integer = 0 To words.Length - 1
        Console.WriteLine(String.Format("Word {0} is {1}", i + 1, words(i)))

    Console.WriteLine("Moving to a new line.")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download