Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

writing and reading to text file 1

Status
Not open for further replies.

mevasquez

Programmer
Aug 26, 2003
75
US
I am having problems trying to read from a file and then writing to the file. I have a file that initially has nothing. When the form loads for the first time, I want to create the file and add some text to it, for example,
Code:
25
SC1=
SC2=
SC3=
SC4=
SC5=
Then close the file.

SC1 through SC5 represent folders and in those folders will be, let's say, five jpg files. I wamt to be able to, when a button is clicked, the first line 25, might be changed to 20. HOw do I just change that one line and keep the other lines the way they are. Eventually, each SC1 through SC5 will have a value.

TIA

Mike V.
 
Try Random Access files.

The records are fixed length so you can read and write from/to the middle of a file.
There will be examples in help.
 
Text files are not fixed length. The only way to modify text in a text file is to read the file, modify the text and rewrite the file. You can read the entire file into a string variable at once, split it and modify each element of the resulting array, then write the elements (or join the array back into a string and write it), read it one line at a time, modify lines that need modification and write them out, or any number of other ways, but you can not modify a text file without having the text in memory (or knowing exactly what, character-by-charcter, is in the file, which is the same thing.)
 
What you are attempting is fairly easy if you format the files as INI files
Code:
[MYSTUFF]
TheNumber=25
SC1=
SC2=
SC3=
SC4=
SC5=
Then you can just use the WriteProfileString and GetProfileString APIs to do stuff
Code:
[COLOR=black cyan]' In a Module[/color]
Public Declare Function GetPrivateProfileString _
                         Lib "kernel32" Alias "GetPrivateProfileStringA" _
                             (ByVal lpSectionName As String, _
                              ByVal lpKeyName As Any, _
                              ByVal lpDefault As String, _
                              ByVal lpReturnedString As String, _
                              ByVal nSize As Long, _
                              ByVal lpFileName As String) As Long

Public Declare Function WritePrivateProfileString _
                         Lib "kernel32" Alias "WritePrivateProfileStringA" _
                             (ByVal lpSectionName As String, _
                              ByVal lpKeyName As Any, _
                              ByVal lpString As Any, _
                              ByVal lpFileName As String) As Long
And In Your code
Code:
Sub Command1_Click()
   WritePrivateProfileString "MYSTUFF", "TheNumber", "20", _
                             "C:\MyStuff.Txt"
End Sub

Sub cmdShowSC1_Click()
   Dim success                     As Long
   Dim nSize                       As Long
   Dim Ret                         As String

   Ret = Space$(2048)
   nSize = Len(Ret)
   success = GetPrivateProfileString("MySTUFF", _
                                     "SC1", _
                                     "", _
                                     Ret, _
                                     nSize, _
                                     "C:\MyStuff.Txt")

   If success > 0 Then
       MsgBox Left$(Ret, success)
   Else
       MsgBox "No Value Set for SC1"
   End If
End Sub
 
This is perfect. I tested it and it will definitely save a lot of lines of code because normally, I will just have to check for value of one line and rewrite that one line.

Thanks,

Mike V.
 
To make it even easier, check out Randy Birch's code that hides the gory details of calling the APIs so that the above becomes
Code:
Sub Command1_Click()
   ProfileSaveItem "MYSTUFF", "TheNumber", "20", "C:\MyStuff.Txt"
End Sub

Sub cmdShowSC1_Click()
   MsgBox ProfileGetItem("MySTUFF", "SC1", "C:\MyStuff.Txt")
End Sub
Randy shows those routines as code in the form but I have made them Public Functions in a module and use them in nearly every project that I build.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top