Problem in reading and writing files in VB6
Hey I am writing program in VB, in that I try to open and edit the text file, I am not getting any problem till it opening also there is no problem in closing also but there is problem started when I edit this file and click to save the changes that time I face this problem I am not able to save any changes in this files, can anyone give me code or example code to solve this problem, and also tell me why I face problem in reading and writing files in VB6?
Re: Problem in reading and writing files in VB6
Use following I use this in my project also. It will solve your problem just copy past this code in VB and as code design your GUI.
Quote:
Private Sub Getmem_Click()
Dim varone As String
Open " enter your text file path " For Input As #1
Input #1, varone
Text1.Text = varone
Close #1
End Sub
--
To write to a file, try this:
Private Sub writefile_Click()
Dim strmsg As String
Dim Sname As String
Open " enter your text file path " For Output As #1
strmsg = MsgBox("File sample.txt opened")
Sname = InputBox("Enter Name")
Print #1, Sname
strmsg = MsgBox("Writing a" & Sname & " to sample.txt ")
Close #1
strmsg = MsgBox("File sample.txt closed")
End Sub
Re: Problem in reading and writing files in VB6
Hey I give you short but sweet code to use it and enjoy your project, it will help you to reduce the compile time and also reduce the code of line, every time write short but useful code it will help you to decreasing the compile time of your application.
Quote:
Dim txtstrmtr As String
Open FILNAM For Input As #1
txtstrmtr = Input(LOF(1), #1)
Close #1
Re: Problem in reading and writing files in VB6
Here Open statement gives you to opening access and writing access to any of the file, here you can specify if you want to read it as text, write to it as binary, and so on. In this code you can set locking options, to stop other programs from opening the file.
Use this code it work fine, without any modification
Quote:
Dim filnam as String
Dim fileno as Integer
fileno = FreeFile
Open "C:\Test.txt" For Input As #fileno
Do While Not EOF(fileno)
Input #fileno, filnam
MsgBox filnam
Loop
Close #fileno
Re: Problem in reading and writing files in VB6
Reading/writing files can also be acheived using FILSYSO (FileSystemObject) as shown below. Please note that using FILSYSO requires an extra reference (under "Project"->"References"), and therefore extra files to be installed with your application.
Quote:
Option Explicit
Dim filsys As FileSystemObject
Dim txtstrm As Textxtstrmtream
Set filsys = New FileSystemObject
Set txtstrm = filsys.OpenTextFile("C:\test.txt", ForWriting, True)
txtstrm.WriteLine "This is"
txtstrm.WriteLine "VB Form"
txtstrm.Close
If filsys.FileExistxtstrm("C:\test.txt") Then
Set txtstrm = filsys.OpenTextFile("C:\test.txt")
Do While Not txtstrm.AtEndOfilsystream
MsgBox txtstrm.ReadLine
Loop
txtstrm.Close
End If
Set txtstrm = Nothing
Set filsys = Nothing
End Sub