Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Greetings,

     I'm a old time VBA and Basic programmer. I need a assist with opening and closing files in VB.NET. The CSV and the code is below. Can someone assist me with converting this to VB.NET. I will appreciated it. Thanks 





"James","ll","ddd",473.57,12700.00,0.03,,,"jan"
"Frank","rr","lll",392.15,8600.00,0.04,"GTD","o","feb"
"Nikola","C","ff",366.55,6900.00,0.05,,,"march"
"Harrison","df","rr",360.76,6900.00,0.05,,,"aug"
"Mark","tm","gg",358.60,8200.00,0.04,,,"julu"


open "C:\Users\john\Desktop\data5.txt" for input as 1
open "C:\Users\john\Desktop\output.txt" for output as 2


Input #1,name1,code1,code2,num1,num2,num3,num4,scode1,scode2,date1


'Calcuations section


write #2,Name1, Evaluation


close 1
close 2


What I have tried:

I have tried
FileOpen(filenum, "data5.txt", OpenMode.Input)
Posted
Updated 16-Feb-17 8:05am
Comments
[no name] 15-Feb-17 21:36pm    
https://msdn.microsoft.com/en-us/library/system.io.file.open(v=vs.110).aspx

For example like this :
VB
Imports System.IO



Public Sub LoadData()

        Dim myFile As String = "c:\Data\myFile.DAT"

        Dim fs As FileStream = Nothing
        Dim CSV As StreamReader = Nothing
        Dim rowdata As String

        Try
            ' open the File
            fs = New FileStream(myFile, FileMode.Open, FileAccess.Read)
            CSV = New StreamReader(fs)

            ' load one row
            rowdata = CSV.ReadLine  

            CSV.Close()

        Catch ex As Exception
            CSV.Close()
        End Try

    End Sub
 
Share this answer
 
v2
Reading and writing files in VB.NET is significantly different to VB6 / VBA. It's probably closer to the FileSystemObject[^], which was the only way to read and write files from VBScript.

MSDN has some good documentation:
How to: Read From Text Files in Visual Basic[^]
How to: Write Text to Files with a StreamWriter in Visual Basic[^]

In this case, it looks like you're trying to read from a CSV file. There is a built-in class to help with this, or you can use a third-party library.
How to: Read From Comma-Delimited Text Files in Visual Basic[^]
CsvHelper[^]

So, for example:
VB.NET
Using reader As New Microsoft.VisualBasic.FileIO.TextFieldParser("C:\Users\john\Desktop\data5.txt")
    reader.TextFieldType = FileIO.FieldType.Delimited
    reader.SetDelimiters(",")
    
    Using writer As New StreamWriter("C:\Users\john\Desktop\output.txt")
        While Not reader.EndOfData
            Dim currentRow As String() = reader.ReadFields()
            Dim name1 As String = currentRow(0)
            Dim code1 As String = currentRow(1)
            Dim code2 As String = currentRow(2)
            
            ' Converting strings to numbers :- 
            ' this can throw an exception if the string is not a valid number.
            Dim num1 As Double = Double.Parse(currentRow(3))
            Dim num2 As Double = Double.Parse(currentRow(4))
            Dim num3 As Double = Double.Parse(currentRow(5))
            Dim num4 As Double = Double.Parse(currentRow(6))
            
            Dim scode1 As String = currentRow(7)
            Dim scode2 As String = currentRow(8)
            Dim date1 As String = currentRow(9)
            
            ' Calcuations section
            
            writer.WriteLine("""{0}"",""{1}""", name1, Evaluation)
        End While
    End Using
End Using

Using Statement (Visual Basic)[^]
Double.Parse Method (System)[^]
String.Format Method (System)[^] (explains the funny-looking string passed to the WriteLine method)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900